Clear Filters
Clear Filters

wrong figure is saved with gcf command

3 views (last 30 days)
Mehmet
Mehmet on 2 Sep 2014
Commented: Adam on 2 Sep 2014
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.

Answers (1)

dpb
dpb on 2 Sep 2014
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 Comment
Adam
Adam on 2 Sep 2014
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.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!