Using hold and plotyy in a loop

6 Ansichten (letzte 30 Tage)
Jonathan
Jonathan am 13 Aug. 2015
Kommentiert: Jonathan am 14 Aug. 2015
I am having trouble getting matlab to update plotyy in a loop.
I want extend a line of data as my code advance in time.
This is easily done for a single plot, as shown below for a simplified extract of my code
for year=1:1:year_max
%***Do Calcs
CalcStuff(year)
%***plot
figure(1)
subplot(3,1,[2 3]); cla; hold on;
%some stuff
subplot(311); hold on;
plot(year/1000,sum(State.Ice),'k.');
xlabel('Time (kyrs BP)')
ylabel ('Total Ice Mass')
drawnow;
end;
However, the similar code below for two lines using plotyy() is broken. The first Data point appears and STAYS on the plot (great), but all subsequent data are overwritten each loop (BAD!!).
for year=1:1:year_max
%***Do Calcs
CalcStuff(year)
%***plot
figure(1)
subplot(3,1,[2 3]); cla; hold on;
%some stuff
subplot(311); hold on;
[ax,h1,h2]=plotyy(year/1000,sum(State.Ice),year/1000,CO2ppmv);
set(h1, 'linestyle', '.','color','k'); set(ax(1), 'ycolor', 'k')
set(h2, 'linestyle', '.','color','r'); set(ax(2), 'ycolor', 'r')
hold(ax(1));hold(ax(2));
xlabel('Time (kyrs BP)')
ylabel ('Total Ice Mass')
drawnow;
end;
Any thoughts on how to fix this would be gratefully received!!

Antworten (1)

Walter Roberson
Walter Roberson am 13 Aug. 2015
hold(ax(1), 'on'); hold(ax(2), 'on');
When you do not specify 'on' or 'off' the default is to toggle between on and off
  1 Kommentar
Jonathan
Jonathan am 14 Aug. 2015
Thanks for the suggestion.
It partially fixes the problem. ax(1) will now show all data, but ax(2) does not; despite them both receiving the same hold on command

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Two y-axis 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