Plot multiple figures in background - matlab

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;

Related

Matlab openfig to existing figure

When in Matlab I use openfig(filename); to open a saved figure, it always opens a new window. All the 'reuse' argument does is not load the file when it appears to already be open. However, I wish to open the file into a given figure, and just overwrite its contents. Is there a way to pass a figure handle to openfig, or is there another function that would accomplish this?
So in code, what I would like to do is something along the following lines:
f = figure;
openfig(filename, 'Figure',f);
and then the figure would be displayed in figure f rather than having opened a second figure window.
I think you can arrange something close to what you want with the copyobj function. Here is a try with a docked figure:
% --- Create sample figure
h = figure;
ezplot('sin(x)');
set(gcf, 'Windowstyle', 'docked');
pause
% --- Replace the axes
clf
g = openfig('test.fig', 'invisible');
copyobj(get(g, 'CurrentAxes'), h);
delete(g);
Which gives me a smooth replacement of the axes, without flickering of the figure.
However, I don't know how this would behave with a fullscreen figure, it surely depends on the method you choose. Check also the doc of copyobj in detail, it's not copying everything so you may want to use the legacy option.

How to force drawnow Matlab GUI to draw in new window?

I have used drawnow to draw the characters of the mnist dataset.. which outputs the following output
when i created GUI with matlab and calling drawnow to display images after loading it draws the figure on the open window giving the following output
my question is how to force it to draw in new window ?
drawnow only asks Matlab to flush the event queue and update figure windows; it doesn't determine how and where things are plotted. It's hard to tell since you don't include any code, but in your case it looks like you just plot the character images and the GUI elements into the same figure.
You can control which figure window a graphics operation refers to by setting the "current figure", whose handle is always contained in the variable gcf (graphics: current figure).
You generate a new figure and make it current by calling
figure
If you want to later make this figure current again, you need to save its handle:
fa = figure;
You then make a figure with a given handle current again by
figure(fa)
Some rough sketch of a possible program:
% generate figure windows
fa = figure;
fb = figure;
% plot something in figure a and make the screen update
figure(fa)
plot(...)
drawnow
% put a UI element into figure b and make the screen update
figure(fb)
uicontrol(...)
drawnow

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.

Inhibit Matlab Window Focus Stealing

Is there a way to tell Matlab not to steal window focus (from an external editor) such as Emacs) upon graphical commands such as figure and plot. This would increase my productivity a lot because I often want to continue code development during data (re-)processing.
It is possible, the trick is to not use the figure statement, but to change the current figure directly. This will change the active plot without changing the focus. Typically I do something like this:
function change_current_figure(h)
set(0,'CurrentFigure',h)
Then, all of the figure(h) statements need to be changed to change_curent_figure(h).
Note, this is included in the matlab documentation.
It should be noted, this only works if the figure is already created. If new figures are going to be periodically created, one could create the figures as the very first few lines of code, save the handles, do the processing, and then plot to them. This example would work. Note, the drawnow command will flush the event buffer, making sure all figures are plotted.
I've seen this work from 2007-2010, not sure if the latest or earlier versions support this, although I have no reason to suspect they don't.
fig1=figure;
fig2=figure;
drawnow;
[a b]=do_complex_processing;
change_current_figure(fig1)
plot(a);
change_current_figure(fig2)
plot(b);
I've got the same question, with the additional complexity that the code creating figures came from an external supplier, and I didn't want to modify it. Here are two possibilities (identified with the help of MathWorks support) tested on Matlab 2014b:
1. Generate the figures without showing them, then show them after the code completion
set(0, 'DefaultFigureVisible', 'off');
for i = 1:10
fprintf('i: %g\n', i)
figure;
pause(1);
end
set(0, 'DefaultFigureVisible', 'on');
figHandles = findall(0, 'Type', 'figure');
set(figHandles(:), 'visible', 'on')
This code does exactly what's needed, but the added inconvenience is that you cannot see any progress of your code run, thus being unable to interrupt a long run if something goes wrong.
2. Dock the figures
Create a new figure:
figure
Dock it:
This will put the figure into the Matlab IDE window.
Make new figures docked and run the code:
set(0, 'DefaultFigureWindowStyle', 'docked');
for i = 1:10
fprintf('i: %g\n', i)
figure;
pause(1);
end
set(0, 'DefaultFigureWindowStyle', 'normal');

How to create a new figure in MATLAB?

Usually when I plot in MATLAB, it always draws on the same figure. How do I make it draw in a new figure?
I know it is pretty elementary, but I'm not finding it using Google Search.
figure;
plot(something);
or
figure(2);
plot(something);
...
figure(3);
plot(something else);
...
etc.
While doing "figure(1), figure(2),..." will solve the problem in most cases, it will not solve them in all cases. Suppose you have a bunch of MATLAB figures on your desktop and how many you have open varies from time to time before you run your code. Using the answers provided, you will overwrite these figures, which you may not want. The easy workaround is to just use the command "figure" before you plot.
Example: you have five figures on your desktop from a previous script you ran and you use
figure(1);
plot(...)
figure(2);
plot(...)
You just plotted over the figures on your desktop. However the code
figure;
plot(...)
figure;
plot(...)
just created figures 6 and 7 with your desired plots and left your previous plots 1-5 alone.
The other thing to be careful about, is to use the clf (clear figure) command when you are starting a fresh plot. Otherwise you may be plotting on a pre-existing figure (not possible with the figure command by itself, but if you do figure(2) there may already be a figure #2), with more than one axis, or an axis that is placed kinda funny. Use clf to ensure that you're starting from scratch:
figure(N);
clf;
plot(something);
...
As has already been said: figure will create a new figure for your next plots. While calling figure you can also configure it. Example:
figHandle = figure('Name', 'Name of Figure', 'OuterPosition',[1, 1, scrsz(3), scrsz(4)]);
The example sets the name for the window and the outer size of it in relation to the used screen.
Here figHandle is the handle to the resulting figure and can be used later to change appearance and content. Examples:
Dot notation:
figHandle.PaperOrientation = 'portrait';
figHandle.PaperUnits = 'centimeters';
Old Style:
set(figHandle, 'PaperOrientation', 'portrait', 'PaperUnits', 'centimeters');
Using the handle with dot notation or set, options for printing are configured here.
By keeping the handles for the figures with distinc names you can interact with multiple active figures. To set a existing figure as your active, call figure(figHandle). New plots will go there now.
Another common option is when you do want multiple plots in a single window
f = figure;
hold on
plot(x1,y1)
plot(x2,y2)
...
plots multiple data sets on the same (new) figure.
As simple as this-
figure, plot(yourfigure);