I have animated the motion of N points in 1D. The problem is that I cannot find a way to get rid of the previous plots and remove the tracks created in the motion.
function sol=Draw(N)
%N is the number of points
PointsPos=1:N
for s=1:1000
for i=1:N
PointsPos(i)=PointsPos(i)+ rand(1,1)
%The position of the point is increased.
end
for i=1:N
%loop to draw the points after changing their positions
hold on
plot(PointsPos,1,'.')
end
pause(0.005)
%here I want to delete the plots of the previous frame s
end
end
A general guideline for MATLAB procedural animation is:
Avoid creating or deleting graphical objects as much as possible in the animation loop.
Therefore, if you invoke plot
image
surf
or delete
in the animation loop, then most likely you are not doing it optimally.
Here, the best practice is to create the plot BEFORE the animation loop, then use set(plot_handle, 'XData', ...) to update the x coordinates of the plot points.
Also you should add a rand(1, N) to PointsPos
, as opposed to adding rand(1, 1) N times.
So you code should look somewhat similar to the following:
function sol=Draw(N)
PointsPos=1:N
h = plot(PointsPos, ones(1, N), '.');
for s=1:1000
PointsPos=PointsPos+ rand(1,N)
set(h, 'XData', PointsPos);
pause(0.005)
end
end