Close/pause process in Matlab - matlab

I have a code with a lot of plots. The problem (excuse my ignorance because I don't know if it's possible) is, for example, when I execute since the beginning, I directly see the last plot, not one after the other. So, for example, I've tried this but it doesn't work at all:
pause(2); %After two seconds it starts and open the plot but I directly see the last plot, not this
plot (x, y);
title ('Average values')
close; % The command close it works but only if I press 'evaluate function'
pause(2);
plot (out1,out2);
close;
Also, I've tried with the keyboard command to try if it's possible to close the plot with one key and then, open the other with another key but I couldn't do it.
If someone knows how can I do it I will be so I'll be so grateful,

Matlab does plotting and the calculation typically in the same process. So typically you will not get anything displayed till there is some spare time for plotting in your program.
To force matlab to redraw the windows you can use the drawnow command. But it only draws exactly at the moment - so if your figure window would be hidden or behind some other window, the redrawing when it comes to foreground will not happen till the next time.
In your case you also close the plot before the pause (where it could be displayed). So if you'd exchange the two commands you should see it. The obvious drawback of the pause is - it halts your program for the time.
from my experience I would suggest you rather save the plots as graphic files and use some external program to view them.
Also I find the popping up new windows annoying and interrupting my workflow - so I would reuse the graphic window, by just clearing it with clf.

I may misunderstand what you are trying to do, but when i try to create what you describe it just works for me as expected. Here is my example:
Note that you will want to close any open figure windows to ensure that it pops up rather than letting it stay in the background.
pause(2); % Wait 2 seconds before starting
plot(1:10); % Plot an upward line
title('up'); % Give it a title
pause(2); % Wait 2 seconds before showing the next plot
plot(10:-1:1); % Plot an downward line
title('down'); % Give it a title
Depending on how you want to use them, saving the plots may be a nicer solution.

Related

Figure legend doubles Matlab code execution time

I noticed that if I have Matlab code where my figure has to display a legend, the running time increases significantly.
Here's an example:
clear all
close all
clc
% legend test
x = [0:1:100];
y = x.^(3.123);
figure('Name', 'X-Y and X-X plot')
plot(x,y)
hold all
plot(x,x)
legend('1', '232')
Gives a running time of 1.1 seconds. Same code without the legend('1', '232') line has an execution time of 0.4 seconds. I find it very odd that a simple legend increases the running time this much.
With the profiler I found that the function mainly responsible for the time increase is called graphics/private/texmex. It has a self-time of 0.12 seconds and is called 4 times. When I don't create a legend this function is not called.
Is there a way to speed up my code, while still generating a legend in a figure?
I'm running 64-bit Matlab 2012b on Mac OS 10.8.3.
When I run the code in the example with set(0, DefaultTextInterpreter, 'none') the texmex function is called by tex>localCallTeXParser, which is called by scribe.legend.methods>strsize, etc...:
graphics/private/texmex
tex>localCallTeXParser
scribe.legend.methods>strsize
scribe.legend.methods>getsizeinfo
scribe.legend.methods>getsize
scribe.legend.methods
scribe.legend.legend
I had the same issue with a project of my own. I wanted to dynamically update some line plots quickly using a slider, but noticed that the having a legend active really killed the performance I was getting.
I found the solution to my problem here - http://undocumentedmatlab.com/blog/plot-performance/.
From the second listed performance hack, I added the lines
set(gca,'LegendColorbarListeners',[]);
setappdata(gca,'LegendColorbarManualSpace',1);
setappdata(gca,'LegendColorbarReclaimSpace',1);
to my code. I got an error message for the first line of code that was mentioned, so I struck it out above. Regardless though, the other two lines of code made my plots update just as quickly with a legend as they did without the legend being present.
Sounds like legend is using a TeX interpreter (at least, that's what texmex sounds like). In that case, you could try
legend({'1', '232'}, 'Interpreter', 'none');
This will disable the TeX interpreter and therefore may improve the performance. I should probably note that I've never experienced any trouble with the speed of the legend function, so it's probably something specific to your plots and/or MATLAB installation/version.
Edit: I have the feeling that the above will draw the legend with the TeX interpreter first, then disable it and draw it again. Try doing the following before drawing the legend or perhaps before drawing the figure (not sure at which point MATLAB will promote the default properties to an actual figure / axes / legend):
set(0, 'DefaultTextInterpreter', 'none');

MATLAB - display figure only when everything has been plotted

I'm trying to plot a 2d finite element solution and am plotting triangle-by-triangle like so:
for i=1:K
figure(1)
fill3(x,y,z,c)
hold on
end
The problem with this is that when I run the code, it literally draws them all in real time so I can see each triangle being drawn. What I want it to just have the finished figure pop up once it's all done.
My friend has coded the same thing except she's not having this issue at all and we can't find any differences in the code. My computer is very fast so it's not an issue of lag. I'm thinking maybe there's a setting in MATLAB that I'm missing?
Edit: I found the problem. Apparently, putting 'figure(1)' inside the loop makes a huge difference. I timed it with 'tic' and 'toc' and it took 54 seconds with the 'figure(1)' label inside the loop and 2 seconds when it was moved just before the loop. Go figure...
When you start the figure, set the visible property to 'off'; when you are done plotting, set the visibility to 'on'.
h = figure('visible','off');
hold on;
for i=1:K
fill3(x,y,z,c);
end
hold off;
set(h,'visible','on');
Also, I'm not entirely sure how the event queue works in MATLAB, but I'm fairly sure the fact you have a fast computer is effecting this. You can simulate what's happening with your computer by using drawnow on your friends slower comp within the for-loop.

Matlab - Close figure windows

i'm writting some code in Matlab editor, which has about 30 figures. So, when I publish it, it opens 30 figures windows, which is annoying. How do I keep it from opening the windows, but keeping the figures in the published window?
I've tried with close(figure), but then the figures don't show on the published window.
Thanks in advance
The simplest thing to do is close all when you are done with the figures. I'm not sure if that can be part of the script or if you have to run it manually after publishing.
At least the plot command has an option to control figure visibility. So you would write something like
h = plot(... , 'Visible', 'off');
I expect these exist for other graphics objects as well, I know it does for the figure associated with anova.
Edit: The above hides the plot but not the figure itself. To hide the figure immediately after it is created, do
set(gcf, 'Visible', 'off')
close function in matlab does what you want. Read the documentation for more details
To close all the plots at the same time, you could use
close all
To close a particular figure named 'fig5' (for example), you could use
fig5 = scatter(x, y);
close(fig5)
If you use just "close", only the recent figure will close.
Perhaps you want hold on which will plot all of the graphs to the same window?
You can Use subplot(m,n,p) to plot multiple graphs on same figure window.
to outline the solution,
first step is to plot using handler. Use figa=figure; where figa is now handler for figure. If you use multiple, like 30 you said, figures, then figa=figure;figb=figure.......figad=figure;
second step; use the figures for whatever you want to plot in;
it has to be done by revoking the figure, for example
figure(figa);hold on;plot(x1,y1)
figure(figb);hold on;plot(x2,y2)....so on for 30 plots
third set is to save all figures
saveas(figa,'1.fig');saveas(figb,'2.fig');.......so on for 30 plots;
fourth step is to close plots from your monitor
close all;
fifth step is to reopen those figures
openfig('1.fig');openfig('2.fig');.............so on for 30 figs
One suggestion: Use excel to create this long list of figure names and better use separate .m files to avoid bulking your matlab main code.

How to merge two figure files into a single file

This should be a problem with a trivial solution, but still I wasn't able to find one.
Say that I have 2 matlab figures fig1.fig, fig2.fig which I want to load and show in the same plotting window.
What should I do?
I mean, I am pretty sure that I can accomplish the task using some low(er) level graphic command which extracts contents from one image and put them in the second one, nonetheless I cannot believe that there is not any high level function (load fig2 on top of fig1) that does this...Comparing 2 plots (unfortunately already saved) is a very common task, I'd say.
Its not clear if you want to extract data from the figures and compare the data, or if you want to combine the plots from two figures into a single figure.
Here is how you combine two figures into one (if thats what you want to do)..
First load the figures:
fig1 = open('FigureFile1.fig');
fig2 = open('FigureFile2.fig');
Get the axes objects from the figures
ax1 = get(fig1, 'Children');
ax2 = get(fig2, 'Children');
Now copy the hangle graphics objects from ax2 to ax1. The loop isn't neccesary if your figures only have a single axes
for i = 1 : numel(ax2)
ax2Children = get(ax2(i),'Children');
copyobj(ax2Children, ax1(i));
end
Note This example assumes that your figures have the same nubmer of axes and that you want to copy objects from the first axes in the second figure to the first axes on the first figure. Its up to you to figure out the proper indexing if the axes indices aren't lined up.
The answer slayton gave is good. Here's another tip: If you have two plots opened in two separate Matlab figure windows, don't forget you can point-and-click copy the proper plots. Do this by clicking the arrow pointer in the Matlab figure window, and then clicking on the plotted line. Copy the (plotted line, textbox, etc...) object. Then, similarly select the axis in the other Matlab figure window and paste it.
I give this 'silly' solution because it has proven to be useful in in collaboration meetings. Point-and-click copying in front of someone (like your adviser) communicates exactly what curves are being compared, and it prevents you from having to fire up code in front of others.
You can also go to File in the menu, Generate Code, for each plots.
Then copy and paste both in the same mfile, with a "hold on" in between and changing details related to the appearance.
Then run the new m-file.

Plot multiple figures in background

I have a MATLAB code that plots multiple figures at the same time. The general trick to achieve the same is to call figure(figHandle) and then make a call to plot, for e.g.
figure(h1);
plot(...args...);
figure(h2);
plot(...args...);
However, I want to do this plotting without bringing figures into foreground everytime I make a call to figure(figHandle). All I want to do is to plot multiple figures simultaneously without bringing them into visibility and export them to an excel sheet (I have figured out exporting to excel sheet part) in the end.
The problem is there are too many figures (around 100) and I have to manually close them down. All I want in the end is an excel sheet with figures exported.
Thanks.
If the problem is to close all the figures you can just use the command close all.
I agree with the solution of mola (+1). However, if you did for some reason wish to keep the figures available after exporting to excel, but don't want them visible on screen, just set the visible property of the figure to off when creating it:
fig1 = figure('visible', 'off')
And if you suddenly decide you need to see it:
set(fig1, 'visible', 'on')
EDIT: It just occurred to me, that if you don't care about ever seeing the figures in matlab, then you should definitely be setting the visible property of the figure to off when you create it. This should significantly speed up the runtime of your code. For example:
x = (1:100)';
tic
for i = 1:1:10
fig1 = figure('visible', 'off');
plot(x);
end
close all
toc
takes 0.27 seconds to run on my machine, but if I switch 'off' to 'on', the runtime increases to 0.65 seconds.
Assign figure handles like
fig1 = figure
run
close figure1
to close figure1 when you’re through with it. Also, if you want to plot multiple things in one figure by tiling, use the subplot function.
When I run Matlab from terminal, and I want to generate a bunch of plots to be saved in an html file, I run this function I wrote, passing the script of interest as an argument, and simply set it and forget it:
function directoryOutput = cliPub(scriptName)
clc;
close all;
fprintf('Publishing...\n\n');
directoryOutput = publish(fullfile(pwd, scriptName), 'figureSnapMethod', 'getframe', 'useNewFigure', false);
close all;