gcf repeatedly returns gui and not newly created figures

Hi all,
I am using a GUIDE-created gui to run a Simulink model and part of the output is a series of figures. I am creating the figures in a loop and printing each one to file within the same loop, as follows:
foldname = char("pitch_bounce_plots " + datestr(now,'yyyy_mm_dd_HH_MM_SS'));
mkdir(foldname);
for k = 1:18
figure;
surface(X,Y,charvalues(:,:,k));
view(3);
xlabel(xstring);
ylabel(ystring);
zlabel(charlabels(k));
grid on;
flname = foldname + "\figure_" + string(get(gcf,'Number'));
print(flname,'-dpng');
end
Each of the 18 plots is presented to me on screen one at a time, each overlaying the last. And yet, in the folder I have 18 images of the gui! Presumably, gcf is not referring to what I consider to be the current figure. What is also slightly weird is that the first image has all grey buttons and the other 17 have the gui run button highlighted in blue.
How do I ensure that my print command prints the figure that is on top at the time?
Eventually, I may make all the plot figures invisible and only print them to file, but for now I would like to see them.
Regards,
Simon.

 Accepted Answer

How do I ensure that my print command prints the figure that is on top at the time?
Simple, don't rely on the current figure since as you've discovered the current figure may not be the one you expect. Instead explictly capture the figure handle of the figure you want to print when you create it:
for k = 1:18
hfig = figure; %capture figure handle. Doesn't matter what gcf is, hfig will always be the figure we created
%...
flname = foldname + "\figure_" + hfig.Number));
print(hfig, flname, '-dpng');
end
Since the current figure can change, it may also be safer if you ensure that all your plots were directed to the correct figure/axes, so:
for k = 1:18
hfig = figure;
hax = axes(hfig); %guaranteed to be in the correct figure
surface(hax, X,Y,charvalues(:,:,k)); %guaranteed to be in the correct axes
view(hax, 3);
xlabel(hax, xstring);
ylabel(hax, ystring);
zlabel(hax, charlabels(k));
grid(hax, 'on');
flname = foldname + "\figure_" + hfig.Number));
print(hfig, flname, '-dpng');
end

6 Comments

That's great Guillaume, many thanks. Such a simple change to insert the figure handle in the print command and yet there is absolutely no reference to using an explicit object name in the Matlab help page for that command.
Regards,
Simon.
help print
includes the statement: " The documentation contains additonal details and examples, including how to specify the figure or model to print"
and the option is listed (admitedly as one of the last options) in
doc print
print(fig,___) saves or prints the figure or Simulink® block diagram specified by fig.
print(fig,___)
is documented syntax .
The simple rule is every graphics function should take, as the first input, the figure or axis or other graphics object to be operated on. If you don't see it in the documentation , try it anyway and report an enhancment request back to MathWorks.
Tools such as bode() often do not support providing axes or figure choice .
Yes, some of the older "full function" plotting tools create a new figure and don't return a handle. In this case I belive bodeplot was added to address those issues.

Sign in to comment.

More Answers (1)

Apologies all. When I read the text following print(fig,_) I took onboard the words 'Simulink block diagram' and somehow missed the reference to 'figure'. I'm surprised that line isn't more prominent in the list of print syntax options.

Categories

Products

Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!