# animatedline fails after the first iteration

1 view (last 30 days)
Youstina E on 6 Aug 2020
Commented: Walter Roberson on 7 Aug 2020
Hi,
It was previously working but now I am getting an empty plot in the second subplot after the first iteration. Code is as follows:
for i=1:10:length(tu)
ax1= subplot(2,1,1)
pcolor(lontemp(findnearest(150,lontemp):findnearest(160, lontemp)),lattemp, hc')
Y1=get(gca,'ylim');
set(ax1,'dataaspectratio',[.7 cos((Y1(2)+Y1(1))/5/180*pi) 1], 'position',[0.204,0.5,0.55,0.42])
ax2=subplot(2,1,2)
if i==1
curve1=animatedline;
curve2=animatedline;
curve3=animatedline;
curve2.Color='blue';
curve3.Color='red';
set(ax2,'Xlim',[1 365],'Ylim', [min(T1) max(T1)], 'Position',[0.13,0.11,0.775,0.29], 'plotboxaspectratio',[1,0.31,0.31])
grid on
hold on
drawnow;
pause(0.1)
else
grid on
drawnow;
pause(.1);
end
end

#### 1 Comment

Youstina E on 6 Aug 2020
just to clarify, the addpoints code works fine in the loop as long as it's not in a subplot.

Walter Roberson on 6 Aug 2020
Create the subplot and modify their position before the loop. Make sure to pass the axes to the hold() and grid() calls.
At present you keep calling subplot and then modify the position. However each time you call subplot it figures out where the plot belongs based on the n m index parameters, and if it finds any axes that over overlap those coordinates but not exactly the same coordinates then it deletes the old one. So subplot() all your axes into existence first before you set the position of any of them, and record the axes handles and do not subplot in a loop unless you do not mind old axes potentially being deleted.

Show 1 older comment
Youstina E on 7 Aug 2020
Sorry, in the pcolor, it should read:
pcolor(lontemp(findnearest(150,lontemp):findnearest(160, lontemp)),lattemp, squeeze(hc(:,:,i))')
dpb on 7 Aug 2020
pcolor as above would be totally immaterial to subplot(2,1,2)
Does not the above generate the expected lines?
Walter Roberson on 7 Aug 2020
hAx1 = subplot(2,1,1);
hAx2 = subplot(2,1,2);
pcolor(hAx1, lontemp(findnearest(150,lontemp):findnearest(160, lontemp)),lattemp, squeeze(hc(:,:,i))');
Y1 = ylims;
%postpone setting the Position of the subplots until all subplots are created
set(hAx1, 'dataaspectratio', [.7 cos((Y1(2)+Y1(1))/5/180*pi) 1], 'position', [0.204,0.5,0.55,0.42])
set(hAx2, 'Xlim', [1 365], 'Ylim', [min(T1) max(T1)], 'Position', [0.13,0.11,0.775,0.29], 'plotboxaspectratio', [1,0.31,0.31])
grid(hAx2, 'on')
hold(hAx2, 'on')
curve1 = animatedline(hAx2);
curve2 = animatedline(hAx2,'color','blue');
curve3 = animatedline(hAx2,'color','red');
for i = 1:10:length(tu)
drawnow;
pause(0.1)
end

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by