Matlab - Close figure windows - matlab

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.

Related

Disallow MATLAB to take focus automatically [duplicate]

This question already has answers here:
Inhibit Matlab Window Focus Stealing
(2 answers)
Closed 3 years ago.
I have the following problem: in my MATLAB code I use statements like
figure(1)
to change destination figure for some data. The problem is that after this MATLAB take system focus on the window with this figure.
When I run a big script in the background and try to do something else on my computer, MATLAB always takes focus and I can't do something normally.
Is there a way to disallow MATLAB to do this? I'm working in Linux Ubuntu.
"Smart"/Silent Figure by Daniel Eaton.
You could do this by making the figure invisible (visible off) at creation, and only making it visible when you want to show it.
For example:
f = figure('Visible', 'off'); %create an invisible figure
plot(rand(1,15)); %plot some stuff to it.
saveas(f, 'test.png', 'png'); %write out the image as a png
close(f); %destroy the figure
Alternatively:
set(f, 'Visible', 'on'); %display a previously invisible figure
Note, if you save the figure as a Matlab .fig file, it will also save the fact that it is invisible, which can be a bit confusing.
In R2018a, the figure property "WindowState" was introduced, see https://blogs.mathworks.com/pick/2018/07/13/maximize-your-figures/
Using this, you can do
set(0, 'DefaultFigureWindowState', 'minimized');
before running the actual script, and this will cause all "standard plots" to not steal focus and be opened in minimized state.
There are functions that still steal focus. I did not investigate in detail, but I believe it's mainly automatic plotting functions such as psd, hist etc. without output arguments. If you call plot yourself you should be fine.
This is untested, but based on the link to the smart figure, it looks like all you need to do to make your figure isn't stealing focus is this:
set(0, 'CurrentFigure', h);
And by the way, if you didn't know, the 0 is meaning "root"

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;

Close/pause process in 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.

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);