MATLAB: Can you use animatedline with subplot, using addpoints (MATLAB2016a)

addpointsanimated lineanimatedlinedrawnowsubplot

I am trying to use addpoints (with animatedline) in order to create an animated figure in a subplot. The code works on its own, but as soon as it's added to a subplot, it only plots the axes.
% Create random signals
time = 1:100;
signal_1 = rand(100,1);
signal_2 = rand(100,1);
% For plotting correlation line..need all values first
corr_line = animatedline;
for start_point = 0:80
sig1_cut = signal_1(start_point+1:start_point+1+19);
sig2_cut = signal_2(start_point+1:start_point+1+19);
corr_pre = corrcoef(sig1_cut,sig2_cut);
corr_fin(start_point+1) = corr_pre(2,1);
end
x_plot = 0:80;
n = 1;
subplot(3,1,1)
axis([-0.1 80 -1 1])
for start_point = 0:80
addpoints(corr_line,x_plot(n),corr_fin(n))
drawnow
n = n+1;
pause(0.01)
end
Thanks!

Best Answer

Yes you can! The function animatedline uses your current axis to work in. In this case there isn't one so it opens a new figure to plot it in, Then when you later call subplot it erases that axis.
To have it work in a subplot, move your your subplot command to before the animatedline bit of code, so the animated line will now be plotted in the subplot instead.
% Create random signals
time = 1:100;
signal_1 = rand(100,1);
signal_2 = rand(100,1);
% For plotting correlation line..need all values first
subplot(3,1,1)
corr_line = animatedline;
for start_point = 0:80
sig1_cut = signal_1(start_point+1:start_point+1+19);
sig2_cut = signal_2(start_point+1:start_point+1+19);
corr_pre = corrcoef(sig1_cut,sig2_cut);
corr_fin(start_point+1) = corr_pre(2,1);
end
x_plot = 0:80;
n = 1;
axis([-0.1 80 -1 1])
for start_point = 0:80
addpoints(corr_line,x_plot(n),corr_fin(n))
drawnow
n = n+1;
pause(0.01)
end