Using hold and plotyy in a loop

2 views (last 30 days)
Jonathan
Jonathan on 13 Aug 2015
Commented: Jonathan on 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!!

Answers (1)

Walter Roberson
Walter Roberson on 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 Comment
Jonathan
Jonathan on 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

Sign in to comment.

Categories

Find more on Two y-axis 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!