1
votes

I am trying to plot with hidden Matlab figures to speed up my plotting:

a=1:10; b=-a;
% make invisible plot window 
f = figure('visible','off');
g = figure('visible','off');

% figure makes the plot visible again    
figure(f)    
plot(a)
saveas(f,'newout','fig')

figure(g)
plot(b)
saveas(g,'newout2','fig')

%% load saved figures    
openfig('newout.fig','new','visible')
openfig('newout2.fig','new','visible')

The problem is the figure(f) command that makes the plot window visible again. The code plots without figure window when I only have one plot and figure(f) is not called.

3
see the tips section in the figure's help (mathworks.com/help/matlab/ref/figure.html)marsei

3 Answers

4
votes

I just learned that instead of calling figure(f) one should use set:

set(0, 'currentfigure', g);

This will change the current figure handle without changing its visibility. The corrected version works as expected:

a=1:10; b=-a;
% make invisible plot window 
f = figure('visible','off');
g = figure('visible','off');

% figure makes the plot visible again    
set(0, 'currentfigure', f);
plot(a)
saveas(f,'newout','fig')

set(0, 'currentfigure', g);
plot(b)
saveas(g,'newout2','fig')

%% load saved figures
close all
openfig('newout.fig','new','visible')
openfig('newout2.fig','new','visible')
0
votes

I would suggest making a whole new plot window. By doing that you will get two different plots, and as you say that only one plot works, i think it could work.

0
votes

An alternative solution is to reference the axes within the plot command (instead of changing the current figure before plot):

a=1:10; b=-a;
f = figure('visible','off');
fax = gca; %// get handle to axes of figure f
g = figure('visible','off');
gax = gca; %// get handle to axes of figure g

plot(fax, a) %// plot in axes of figure f
saveas(f,'newout','fig')

plot(gax, b)  %// plot in axes of figure g
saveas(g,'newout2','fig')

openfig('newout.fig','new','visible')
openfig('newout2.fig','new','visible')