animation: point following line in 2d plot

Hi, I think i have a fairly easy question but i just can't get it to work. I am trying to create a plot of a 2d line, and i want a point to slowly follow that line. My code so far is:
figure
plot(y)
t=(length(y)+1);
i=1;
while i<t
hold on
plot(i,y(i),'o m');
hold off
i=i+1;
pause(0.1);
end
The problem is that all the points are drawn, but how can i delete the previous point (y(i-1))? Thanks!

Answers (1)

here is one way:
x = 0:pi/50:2*pi;
y = sin(4*x);
plot(x,y)
hold on
h = plot(x(1),y(1),'mo','MarkerFaceColor','m',...
'YDataSource','Y',...
'XDataSource','X');
for t = 1:numel(x)
X = x(t);
Y = y(t);
refreshdata(h,'caller');
drawnow;
pause(.1);
end

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 19 Oct 2011

Community Treasure Hunt

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

Start Hunting!