Using hold and plotyy in a loop

2 ビュー (過去 30 日間)
Jonathan
Jonathan 2015 年 8 月 13 日
コメント済み: Jonathan 2015 年 8 月 14 日
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!!

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 8 月 13 日
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 件のコメント
Jonathan
Jonathan 2015 年 8 月 14 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTwo y-axis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by