MATLAB: How to update and connect the plotted datapoints during a for loop in MATLAB 7.9 (R2009b)

loopMATLABplotupdate

I am using MATLAB 7.9 (R2009b) to plot data in a for loop.
figure; title('Plot during execution');
A = rand(1,10);
B = rand(1,10);
N = length(A);
for idx=1:N
plot(A(idx),B(idx), '*-');
end
With this code the plot does not update in the for loop.

Best Answer

To update the plot during the for loop, you can insert a short pause after the plot command. Alternatively, execute
drawnow
to force the plot to update in each iteration of the for loop.
To plot linked datapoints, the points must be in a vector which is plotted with a single PLOT command. For example:
figure; title('Plot during execution');
A = rand(1,10);
B = rand(1,10);
N = length(A);
for idx=1:N
plot(A(1:idx),B(1:idx), '*-');
pause(0.1)
end
Related Question