Hi
I'm trying to make an animation, where I only display one point at the time, right now the trajectory stays, and I want only one point displayed
any suggestions, i want it to look like this: https://www.youtube.com/watch?v=q1dqIcLi3N0
this is my code so far
x= Ball(:,1);
y= Ball(:,2);
%%
figure
curve = animatedline('marker','*','Color','r');
set(gca,'Xlim',[-6000 6000], 'Ylim',[-4000 4000])
for i= 1:length(Ball)
addpoints(curve,x(i),y(i));
drawnow
end

 채택된 답변

Adam Danz
Adam Danz 2019년 2월 28일
편집: Adam Danz 2019년 2월 28일

1 개 추천

If your task is that simple, I prefer updating the XData and YData rather than using the animatedline method. This is faster and more efficient. Instead of deleting and drawing a new point on each iteration, it merely changes the coordinate value of the point that is already drawn. If this is too fast, you can control the approximate timing by using pause() within the loop to slow it down.
Here's an example (just run this to watch a dot move along a sinusoidal trajectory).
x= 0:0.1:2*pi;
y= sin(x);
figure
ph = plot(x(1), y(1), 'r*'); %plot initial point
set(gca,'Xlim',[0 6.5], 'Ylim',[-1 1])
for i=1:length(y)
ph.XData = x(i); %change x coordinate of the point
ph.YData = y(i); %change y coordinate of the point
drawnow
pause(0.05) %control speed, if desired
end
If you prefer to use the animatedline method, just insert clearpoints() before you call addpoints().
for i= 1:length(Ball)
clearpoints(curve)
addpoints(curve,x(i),y(i));
drawnow
end

댓글 수: 5

Morten Jørgensen
Morten Jørgensen 2019년 2월 28일
thanks for the help!
is this also possible to do with a scatterplot, where i plot 6 different points ?
Adam Danz
Adam Danz 2019년 2월 28일
편집: Adam Danz 2019년 3월 12일
Yes. You can use this method with any graphic object that has the XData and YData (and ZData) properties. You're just replacing the coordinates of an object with new coordinates.
Lorenzo
Lorenzo 2025년 4월 8일
any way to plot not only the "last" point but also the line up to the last point (Not the entire line, the sin function in this example) Thank you
Adam Danz
Adam Danz 2025년 4월 21일
편집: Adam Danz 2025년 4월 21일
ph = plot(nan(size(x)), nan(size(y)), 'r*'); %plot initial point
set(gca,'Xlim',[0 6.5], 'Ylim',[-1 1])
for i=1:numel(y)
ph.XData(i) = x(i); %change x coordinate of the point
ph.YData(i) = y(i); %change y coordinate of the point
drawnow
pause(0.05) %control speed, if desired
end
Walter Roberson
Walter Roberson 2025년 4월 21일
편집: Walter Roberson 2025년 4월 21일
An alterative animatedline approach to the original question is
curve = animatedline('MaximumNumPoints', 1);
for i= 1:length(Ball)
addpoints(curve,x(i),y(i));
drawnow
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Animation에 대해 자세히 알아보기

질문:

2019년 2월 28일

편집:

2025년 4월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by