wrong figure is saved with gcf command

I run a simulation from m file. In the simulation, I use some lookup tables. I would like to save some simulation results for different speed and torque values. But, the output is not the plots I wanted. It saves the lookup table (which is plotted before) as .bmp.
This is the gcf part of my code.
saveas(gcf, ['.\caps\currentsDQ', ...
['CurrentDQ @ Speed=' num2str(SpeedCounter) ...
'RMS & Torque =' num2str(Torque_Demand) 'Nm'], '.bmp']);
Ps. this is my first post. if the question is not clear, I am sorry.

回答(1 个)

gcf, as the name implies, returns the handle of the current figure. The current figure is the one that has focus at the time the call is executed (which generally will be the last one referenced in the script/function).
To ensure you save the desired figure, don't rely on gcf but save the handle for the figure that you do want to save when it is created.
hFig=figure; % create a new figure, save the handle
plot(... % make the plot...
Then when you're ready for the saveas call, use the above handle --
saveas(hFig, ...

1 个评论

I second that. I only ever use gcf when I am doing some quick stuff on command line or a script where the command using gcf is on the same line as the command creating the figure so it can't possibly refer to the wrong figure.
In all other situations I keep track of my own figure handles as dpb suggests.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Animation 的更多信息

标签

提问:

2014-9-2

评论:

2014-9-2

Community Treasure Hunt

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

Start Hunting!

Translated by