Using hold and plotyy in a loop
2 views (last 30 days)
Show older comments
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!!
0 Comments
Answers (1)
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
See Also
Categories
Find more on Two y-axis 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!