animate 2d plot

6 views (last 30 days)
Nur Amalina
Nur Amalina on 3 Aug 2020
Commented: Nur Amalina on 4 Aug 2020
Hi, All!
Say i have 2 variable, x1 and x2. I wanna plot (x1, x2) and animate it over time. i've tried this code:
x = [1 2;2 1;3 1;1 2;2 1;3 2];
x1 = x(1:end,1);
x2 = x(1:end,2);
r=animatedline;
for t=1:1000
for i=1:6
x1(i,t+1)=x1(i,t)+3;
x2(i,t+1)=x2(i,t)+x1(i,t);
end
addpoints(r,x1(1,:),x2(1,:))
addpoints(r,x1(2,:),x2(2,:))
drawnow
end
But that's not what i meant. From code above, the first line move from (1,2) to (4,3) to (7,7).... the second line move from (2,1) to (5,3), to (8,8),...
till the figure show the movement of those 6 lines.
  4 Comments
jonas
jonas on 3 Aug 2020
I dont understand what you want to do. Do you want to animate six or two lines?
Nur Amalina
Nur Amalina on 4 Aug 2020
6 lines @jonas. i was trying to make 2 lines first but it didn't work

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 3 Aug 2020
Edited: Cris LaPierre on 3 Aug 2020
Here's one way to do it following the instructions on the Line Animations documentation page. I reduced the number of points to 100.
x = [1 2 3 1 2 3];
y = [2 1 1 2 1 2];
l1 = animatedline;
l2 = animatedline;
l3 = animatedline;
l4 = animatedline;
l5 = animatedline;
l6 = animatedline;
axis([0 150 0 1500])
for t = 1:100
if t>1
x(t,:)=x(t-1,:)+3;
y(t,:)=y(t-1,:)+x(t,:);
end
addpoints(l1,x(t,1),y(t,1))
addpoints(l2,x(t,2),y(t,2))
addpoints(l3,x(t,3),y(t,3))
addpoints(l4,x(t,4),y(t,4))
addpoints(l5,x(t,5),y(t,5))
addpoints(l6,x(t,6),y(t,6))
drawnow limitrate
end

Categories

Find more on Animation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!