Colored table in Matlab - matlab

I am trying to figure out how to generate a colored chart on a matlab, like the one you can find in here on page 9. (You will have to look through it to find what I am referring to - Stackoverflow doesn't allow me to post pictures in the postings just yet.)
A few questions:
I do have the table, but my table is a set of discreet points, not a continuous spectrum. So... can I do it in the first place?
If it IS possible, how would I do it?
(By the way, that table is from combat simulation for Risk - I am doing the combat simulation for Risk II, just for fun.)

The type of image you are looking for, as can be seen on Page 9, is a imagesc plot. Here's a simple example, using a double sin function. Done without vectorization for simplicity.
x=0:pi/180:pi;
y=0:pi/180:pi;
output=zeros(length(x),length(y));
for ix=1:length(x)
for iy=1:length(x)
output(ix,iy)=sin(x(ix)*2)*cos(y(iy)*4);
end
end
figure;imagesc(x,y,output)

I think you're looking for the filled contourplot.
See also: http://www.mathworks.nl/help/techdoc/ref/contourf.html

Related

Can automatically enumerate figures or keep tokens in matlab?

In a live script in matlab, I plot multiple figures, and I use this code to enumerate the figures:
FigureQuantity=1
plot(data_1)
title('Figure '+string(FigureQuantity))
Then on another code section I do it again
FigureQuantity=FigureQuantity+1
plot(data_n)
title('Figure '+string(FigureQuantity))
The problem is that if I run the last code section again, FigureQuantity gets updated and the enumeration of figures gets broken.
There is any way to get the number of tokens ordered by his code appearance on the live script? (independent of how many times the section code is run)
I would like to keep tokens so I can mix inserted images and plots. And I want to export the document as PDF (not to show plots in an application or an independent window).
What I need is something like MS Word enumeration of figures and tables.
I found this Matlab documentation: Number Section Headings, Table Titles, and Figure Captions Programmatically, but it appears to be used for creation of MS Word or HTML documents, and not to enumerate images on Matlab live scripts.
I do not understand how to use it, or if that is his purpose on Matlab.
I'm assuming you're updating the data_n variable live as well; otherwise, if you're defining these variables manually then not doing so for the figure variables isn't really the solution I think you're looking for.
Why not for-loop through the figure updates?
for FigureQuantity = 1:numberOfFigureQuantities
figure(FigureQuantity);
hold on;
plot(data_n(FigureQuantity))
title(strcat('Figure Number: ',num2str(FigureQuantity)));
end
The figure count corresponding to the FigureQuantity will index the appropriate figure and will update that figure if it already existed. This is the solution I think you're looking for; if not, please clarify.

Inner workings of Google's Quick Draw

I'm asking this here because I didn't find anything online.
I would be interested in how Google Quick Draw works, specifically:
1) How does it output the answer - does it have a giant output vector with a probability for each type of drawing?
2) How does it read the data - I see they've implemented some sort of order aware input system, but does that mean that they input positions of the interpolated lines that users draw? This is problematic because it's variable length - how did they solve it?
3) And, finally, which training algorithm are they using? The data grows each time someone draws something new, or do they just feed it into the algorithm when it's created?
If you know any papers on this or by miracle you work at Google and/or can explain how it works, I would be really greatful. :)

Normalized histogram in MATLAB incorrect?

I have the following set of data:
X=[4.692
6.328
4.677
6.836
5.032
5.269
5.732
5.083
4.772
4.659
4.564
5.627
4.959
4.631
6.407
4.747
4.920
4.771
5.308
5.200
5.242
4.738
4.758
4.725
4.808
4.618
4.638
7.829
7.702
4.659]; % Sample set
I fitted a Pareto distribution to this using the maximum likelihood method and I obtain the following graph:
Where the following bit of code is what draws the histogram:
[N,edges,bin] = histcounts(X,'BinMethod','auto');
bin_middles=mean([edges(1:end-1);edges(2:end)]);
f_X_sample=N/trapz(bin_middles,N);
bar(bin_middles,f_X_sample,1);;
Am I doing this right? I checked 100 times and the Pareto distribution is indeed optimal, but it seems awfully different from the histogram. Is there an error that may be causing this? Thank you!
I would agree with #tashuhka's comment that you need to think about how you're binning your data.
Imagine the extreme case where you lump everything together into one bin, and then try to fit that single point to a distribution. Your PDF would look nothing like your single square bar. Split into two bins, and now the fit still sucks, but at least one bar is (probably) a little bigger than the other, etc., etc. At the other extreme, every data point has its own bar and the bar graph is nothing but a random forest of bars with only one count.
There are a number of different strategies for choosing an "optimal" bin size that minimizes the number of bins but maximizes the representation of the underlying PDF.
Finally, note that you only have 30 points here, so your other problem may be that you just haven't collected enough data to really nail down the underlying PDF.

MATLAB: How to Retrieve Intensity-Based Registration Data (with imregister) to re-Perform Registration?

I thought this should be a simple task, I just can't find the way to do it:
I am using 'imregister' (MATLAB) to register two medical X-ray images.
To ensure I get the best registration outcome as possible, I use some image processing techniques such as contrast enhancement, blackening of objects that are different between images and even cropping.
The outcome of this seems to be quite satisfying.
Now, I want to perform the exact same registaration on the original images, so that I can display the two ORIGINAL images automatically in alignment.
I think that an output parameter such as tform serves this purpose of performing a certain registration on any two images, but unfortunately 'imregister' does not provide such a parameter, as far as I know.
It does provide as an output the spatial referencing object R_reg which might be the answer, but I still haven't figured out how to use it to re-preform the registration.
I should mention that since I am dealing with medical X-ray images on which non of the feature-detecting algorithms seem to work well enough to perform registration, I can only use intensity-based (as opposed to feature-based) registration, and therefore am using 'imregister'.
Does anyone know how I can accomplish this?
Thanks!
Noga
So to make an answer out of my comment, there are 2 things you can do depending on the Matlab release you are using:
Option 1: R2013a and earlier
I suggest modifying the built-in imregister function by forcing tform to be an output and save that function under another name.
For example:
function [movingReg,Rreg,tform] = imregister2(varargin)
save that, add it to your path and you're good to go. If you type edit imregister you will notice that the 1st line calls imregtform to get the geometric transformation required, while the last line calls imwarp, to apply that geometric transformation. Which leads us to Option 2.
Option 2: R2013b and later
Well in that case you can directly use imregtform to get the tform object and then use imwarpto apply it. Easy isn't it?
Hope that makes things clearer!

Using MATLAB's plotting features as an interactive part of a Fortran program

Although many of you will have a decent idea of what I'm aiming at, just from reading the title -- allow me a simple introduction still.
I have a Fortran program - it consists of a program, some internal subroutines, 7 modules with its own procedures, and ... uhmm, that's it.
Without going into much detail, for I don't think it's necessary at this point, what would be the easiest way to use MATLAB's plotting features (mainly plot(x,y) with some customizations) as an interactive part of my program ? For now I'm using some of my own custom plotting routines (based on HPGL and Calcomp's routines), but just as part of an exercise on my part, I'd like to see where this could go and how would it work (is it even possible what I'm suggesting?). Also, how much effort would it take on my part ?
I know this subject has been rather extensively described in many "tutorials" on the net, but for some reason I have trouble finding the really simple yet illustrative introductory ones. So if anyone can post an example or two, simple ones, I'd be really grateful. Or just take me by the hand and guide me through one working example.
platform: IVF 11.something :) on Win XP SP2, Matlab 2008b
The easiest way would be to have your Fortran program write to file, and have your Matlab program read those files for the information you want to plot. I do most of my number-crunching on Linux, so I'm not entirely sure how Windows handles one process writing a file and another reading it at the same time.
That's a bit of a kludge though, so you might want to think about using Matlab to call the Fortran program (or parts of it) and get data directly for plotting. In this case you'll want to investigate Creating Fortran MEX Files in the Matlab documentation. This is relatively straightforward to do and would serve your needs if you were happy to use Matlab to drive the process and Fortran to act as a compute service. I'd look in the examples distributed with Matlab for simple Fortran MEX files.
Finally, you could call Matlab from your Fortran program, search the documentation for Calling the Matlab Engine. It's a little more difficult for me to see how this might fit your needs, and it's not something I'm terribly familiar with.
If you post again with more detail I may be able to provide more specific tips, but you should probably start rolling your sleeves up and diving in to MEX files.
Continuing the discussion of DISLIN as a solution, with an answer that won't fit into a comment...
#M. S. B. - hello. I apologize for writing in your answer, but these comments are much too short, and answering a question in the form of an answer with an answer is ... anyway ...
There is the Quick Plot feature of DISLIN -- routine QPLOT needs only three arguments to plot a curve: X array, Y array and number N. See Chapter 16 of the manual. Plus only several additional calls to select output device and label the axes. I haven't used this, so I don't know how good the auto-scaling is.
Yes, I know of Quickplot, and it's related routines, but it is too fixed for my needs (cannot change anything), and yes, it's autoscaling is somewhat quircky. Also, too big margins inside the graf.
Or if you want to use the power of GRAF to setup your graph box, there is subroutine GAXPAR to automatically generate recommended values. -2 as the first argument to LABDIG automatically determines the number of digits in tick-mark labels.
Have you tried the routines?
Sorry, I cannot find the GAXPAR routine you're reffering to in dislin's index. Are you sure it is called exactly like that ?
Reply by M.S.B.: Yes, I am sure about the spelling of GAXPAR. It is the last routine in Chapter 4 of the DISLIN 9.5 PDF manual. Perhaps it is a new routine? Also there is another path to automatic scaling: SETSCL -- see Chapter 6.
So far, what I've been doing (apart from some "duck tape" solutions) is
use dislin; implicit none
real, dimension(5) :: &
x = [.5, 2., 3., 4., 5.], &
y = [10., 22., 34., 43., 15.]
real :: xa, xe, xor, xstp, &
ya, ye, yor, ystp
call setpag('da4p'); call metafl('xwin');
call disini(); call winkey('return');
call setscl(x,size(x),'x');
call setscl(y,size(y),'y')
call axslen(1680,2376) !(8/10)*2100 and 2970, respectively
call setgrf('name','name','line','line')
call incmrk(1); call hsymbl(3);
call graf(xa, xe, xor, xstp, ya, ye, yor, ystp); call curve(x,y,size(x))
call disfin()
end
which will put the extreme values right on the axis. Do you know perhaps how could I go to have one "major tick margin" on the outside, as to put some area between the curve and the axis (while still keeping setscl's effects) ?
Even if you don't like the built-in auto-scaling, if you are already using DISLIN, rolling your own auto-scaling will be easier than calling Fortran from MATLAB. You can use the Fortran intrinsic functions minval and maxval to find the smallest and largest values in the data, than write a subroutine to round outwards to "nice" round values. Similarly, a subroutine to decide on the tick-mark spacing.
This is actually not so easy to accomplish (and ideas to prove me wrong will be gladly appreciated). Or should I say, it is easy if you know the rough range in which your values will lie. But if you don't, and you don't know
whether your values will lie in the range of 13-34 or in the 1330-3440, then ...
... if I'm on the wrong track completely here, please, explain if you ment something different. My english is somewhat lacking, so I can only hope the above is understandable.
Inside a subroutine to determine round graph start/end values, you could scale the actual min/max values to always be between 1 and 10, then have a table to pick nice round values, then unscale back to the correct range.
--
Dump Matlab because its proprietary, expensive, bloated/slow and codes are not easy to parallelize.
What you should do is use something on the lines of DISLIN, PLplot, GINO, gnuplotfortran etc.