1

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.

1

3 Answers 3

4

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')
Sign up to request clarification or add additional context in comments.

2 Comments

That's good to know! Can you accept your answer so that the thread is closed?
Yes, but only after 2 days.
0

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.

1 Comment

I already have two plots ,f and g, and want to switch between them without changing their visibility.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.