Can automatically enumerate figures or keep tokens in matlab? - 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.

Related

Need a method to store a lot of data in Matlab

I've asked this before, but I feel I wasn't clear enough so I'll try again.
I am running a network simulation, and I have several hundreds output files. Each file holds the simulation's test result for different parameters.
There are 5 different parameters and 16 different tests for each simulation. I need a method to store all this information (and again, there's a lot of it) in Matlab with the purpose of plotting graphs using a script. suppose the script input is parameter_1 and test_2, so I get a graph where parameter_1 is the X axis and test_2 is the Y axis.
My problem is that I'm not quite familier to Matlab, and I need to be directed so it doesn't take me forever (I'm short on time).
How do I store this information in Matlab? I was thinking of two options:
Each output file is imported separately to a different variable (matrix)
All output files are merged to one output file and imprted together. In the resulted matrix each line is a different output file, and each column is a different test. Problem is, I don't know how to store the simulation parameters
Edit: maybe I can use a dataset?
So, I would appreciate any suggestion of how to store the information, and what functions might help me fetch the only the data I need.
If you're still looking to give matlab a try with this problem, you can iterate through all the files and import them one by one. You can create a list of the contents of a folder with the function
ls(name)
and you can import data like this:
A = importdata(filename)
if your data is in txt files, you should consider this Prev Q
A good strategy to avoid cluttering your workspace is to import them all into a single matrix. SO if you have a matrix called VAR, then VAR{1,1}.{1,1} could be where you put your test results and VAR{1,1}.{2,1} could be where you put your simulation parameters of the first file. I think that is simpler than making a data structure. Just make sure you uniformly place the information in the same indexes of the arrays. You could also organize your VAR row v col by parameter vs test.
This is more along the lines of your first suggestion
Each output file is imported separately to a different variable
(matrix)
Your second suggestion seems unnecessary since you can just iterate through your files.
You can use the command save to store your data.
It is very convenient, and can store as much data as your hard disk can bear.
The documentation is there:
http://www.mathworks.fr/help/techdoc/ref/save.html
Describe the format of text files. Because if it has a systematic format then you can use dlmread or similar commands in matlab and read the text file in a matrix. From there, you can plot easily. If you try to do it in excel, it will be much slower than reading from a text file. If speed is an issue for you, I suggest that you don't go for Excel.

Disable plots in Matlab

I have some programs written in Matlab that I need to run several times for some reasons (debugging, testing with different input, etc...)
But, there are a lot's of graphs that are plotted by the programs and its various functions such that everytime I run the program, I have to wait for all the graphs to be displayed, which is very annoying and time consuming (especially when you are working with a small laptop).
After the program is executed, I close them with a close all.
So my question is:
Is there a way to disable all plots/figures/graphs in Matlab? either in the options, or by executing a certain code like disable plot and enable plot to ensure that no figures are being displayed.
I know that I can just browse the code and comment the plotting part, but I don't want to forget to uncomment.
Try some combination of the two commands:
set(gcf,'Visible','off') % turns current figure "off"
set(0,'DefaultFigureVisible','off'); % all subsequent figures "off"
The second one, if you put it near the beginning of your program, might do the trick for you. Of course, it is still creating the plots, which might be undesirable for computation time and/or RAM issues.
This is a classic reason to avoid Matlab when one can. It fosters bad programming design. To solve this problem correctly, you should create something that lets you "flip a switch" at the highest level of your program and control whether plots show or do not show. Perhaps it even has gradations of the show/don't show option so you can select different types of plots that do/do not show depending on what diagnostics you are running.
Ideally, you'd want this "flip a switch" creation to be a class that has access to visibility and plot functions of other objects. But because interactive object-orientation is so cumbersome in Matlab, it's often not worth the effort to develop such a solution, and most people don't think about this design aspect from the outset of their project.
Matlab would encourage someone to solve this by making flag variables like "isPlotVisible" or something, and creating functions that always accept such flags. I agree this is a bad design.
You could run matlab from the command line with:
matlab -nojvm
but then you don't get the GUI at all. Alternatively, you could write a file 'plot.m':
function h = plot(varargin)
h = [];
end
which doesn't do anything. If this is in the working directory (or somewhere else near the top of the path), then plot will call your function instead of the 'real' plot. You'd need to do the same from any other graphing functions you call.
The closest way I know of 'turning off plotting' would be a folder of such functions that you can add to the path to disable plotting, and remove to enable.
The previous methods are fine, but an easy and good habit to take is to use a "on/off parameter". So basically, at the beginning of your code, you can add something like:
DisplayFigure = 1; %1 = display, 0 = no display
After that, add "if DisplayFigure == 1 ... end" for all your plotting related commands, where the commands should be inside the if statement (the ... above). Hence you won't even compute the plots, which will save you a lot of time and memory. You just have to change the value of the variable "DisplayFigure" to plot or not the figures.

Colored table in 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

How do I stop Matlab from displaying matrix content on importdata()

I'm running a series of scripts that all import data from different matrix files. It seems that displaying the content of the matrix is taking a lot of time. Normally I would do "more on" and just cancel the display after the first page, but I am doing things automatically here, from the command-line version.
Is there a way to stop Matlab from displaying the content of variables when it loads them? Say, a non-verbose/daemon mode? I couldn't find a way to do it when I searched, but I'm sure there must be one.
Thanks in advance!
Found it! The answer is to add a semicolon (;) at the end of the line, for example:
m=importdata('matrix.txt');
This will prevent it from printing the contents of m.

Matlab: Write text to PDF

I'd like to create a PDF out of my matlab m-function. The PDF should contain some text information which I want to style a bit and one image (which is previously generated as figure). Is there any way? The only thing I found is publish to publish source-code. The only alternative I could think off was to programm the texts into the figure window and than export the whole figure to pdf. Perhaps there's a better way to do it?
Thanks!
I would recommend generating your figure with Matlab, outputting it to eps, then converting the figure to pdf using epstopdf. Then embed your figure in a latex document and generate a pdf with pdflatex.
I know this sounds like an incredibly roundabout way of doing it, but
This is the way I've come to do it after years of experience and it always gives me the best results
Every one of my colleagues does it this way for the same reason
The results will be completely reproducible
You're using Matlab to do what it's good at (making scientific figures) and latex to do what it's good at (formatting documents)
The Matlab code to make the eps of the figure would be like this (supposing your figure is figure 1):
print -depsc2 -f1 -loose my_fig.eps
You could pretty easily write a latex template that uses my_fig.pdf and then run everything from your Matlab using bangouts:
!epstopdf my_fig
!pdflatex mydoc.tex
If you're on Windows, you can connect to Word using 'actxserver', insert and style any text you like into a blank document, copy and paste MATLAB figures into the document, and then save it to PDF. You can do all of that from within MATLAB. The first time you do this it's a pain, as you need to learn quite a bit about the Word Object Model; but once you've done it a couple of times it's very simple and quick, and you can achieve very professional results. You can combine this with using the 'export_fig' that others have mentioned.