How to update current figure instead of creating a new one?
52 views (last 30 days)
Show older comments
Dear Matlab forum members,
I'm using a loop and hold on to add multiple curves onto the same plot (See code below), and I want the figure to be updated every time I change a parameter and recalculate instead of creating a new figure.
I tried various options (set function, refresh - see triple-commented lines in the code) - none of those works (every time new figure is created). Please suggest how to achieve the goal?
Thanks a lot
THE CODE:
SpecNo = [1,5,7,10,13,15,17,20,23,25];
offset = 0.2;
myfig=figure;
labels = strings(10,1);
for i = 1:length(SpecNo)
x = Data{T}.NormSpectra(SpecNo(i),:) + i*offset;
y = Data{T}.Calibration(SpecNo(i),:);
labels(i) = sprintf('%.1f uW',Data{T}.ExcitationPower(SpecNo(i))*10^6);
myplot=plot(y,x);
%%%set(myplot,'XData',y, 'YData',x);
hold on
end
%%%refresh(myfig);
legend(labels);
-------------------------------------------------------------
1 Comment
Adam
on 20 Aug 2018
Edited: Adam
on 20 Aug 2018
At a glance that seems like it should do what you want, although you should always use explicit axes handles with functions like plot and hold instead of just relying on whatever the current axes is - often it will be what you want so the code will seem fine, then there will be a few occasions when it is not what you expect and you get plots in unexpected places.
In the above case I see no reason why it would keep creating new figures though.
A
drawnow
instruction in the loop will force an update if it just isn't updating your plot.
Answers (3)
Patrick Woryna
on 7 Nov 2020
figure(1) % will bring figure one to foreground if it exists and update this figure
with figure(n) you can select the figure to be updated.
0 Comments
David Neill Asanza
on 21 Oct 2020
For those looking for a way to not get overloaded with new figures as you run the same script multiple times, a simple solution is to add:
close all
To the top of your script. This closes all figures at the start of your script, so you only ever see the latest figures.
This may not be what OP was asking for, but I found this thread when looking for a solution to the above problem. So I hope this helps others with the same problem.
0 Comments
Anton Velychko
on 20 Aug 2018
Edited: Anton Velychko
on 20 Aug 2018
1 Comment
Adam
on 20 Aug 2018
myfig=figure; hAxes = gca;
at the top would do. I don't like using gca in proper code, but there I usually already have an axes and when used as above it is pretty safe as nothing should be able to happen to change which figure has focus between the figure creation instruction and the assignment of the axes handle.
Then, as shown in the help:
plot( hAxes,... )
hold( hAxes, 'on' )
etc etc will use an explicit axes handle. I'm still surprised that, given only this code snippet, you are getting a new figure every time though.
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!