How to update current figure instead of creating a new one?

50 Ansichten (letzte 30 Tage)
Anton Velychko
Anton Velychko am 20 Aug. 2018
Beantwortet: Patrick Woryna am 7 Nov. 2020
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 Kommentar
Adam
Adam am 20 Aug. 2018
Bearbeitet: Adam am 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.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Patrick Woryna
Patrick Woryna am 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.

David Neill Asanza
David Neill Asanza am 21 Okt. 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.

Anton Velychko
Anton Velychko am 20 Aug. 2018
Bearbeitet: Anton Velychko am 20 Aug. 2018
I've added drawnow to the loop (tried couple places), also cleared workspace just in case, the code now is:
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);
drawnow
hold on
drawnow
end
Still the same problem however - Matlab keeps on creating new figures - figure 1, figure 2, figure 3 etc.
SO it's probably to do with axes handles not being explicit, but I'm not sure how to fix that.
  1 Kommentar
Adam
Adam am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by