Plot a normal figure and animatedline together onthe same figure

조회 수: 14 (최근 30일)
Hassan Alkomy
Hassan Alkomy 2021년 1월 6일
편집: dpb 2021년 1월 8일
Hi everyone,
I have a question about how to plot a regular figure (using plot or plot3) and animated lines (using animatedline and addpoints) in the same figure.
I want also to record a video of the entire figure. Can I do that with regular way of getfram and VideoWriter?
Here is an example
t=0:0.01:10;
x=cos(t);
y=sin(t);
z=t;
h=animatedline('Color','r');
k=animatedline('Color','b');
view(3)
for i=1:length(t)
addpoints(h,x(i),y(i),z(i));
addpoints(h,0.5*x(i),0.5*y(i),z(i));
% I want also to plot the following in the same figure
plot3(x,y,z) % I want the graph to apear in full in the begining not as an animated line
end
Can anyone help me with that?
Thank you

채택된 답변

dpb
dpb 2021년 1월 7일
편집: dpb 2021년 1월 8일
...
% I want also to plot the following in the same figure
% I want the graph to apear in full in the begining not as an animated line
% then plot it first, if that is what is desired
plot3(x,y,z)
view(3)
% then must set hold on to add to an axes or will clear existing stuff
hold on
h=animatedline('Color','r');
for i=1:length(t)
addpoints(h,x(i),y(i),z(i));
addpoints(h,0.5*x(i),0.5*y(i),z(i));
pause(0.01)
end
This fills the area between the outer and inner lines; not clear to me why it appears to be solid surface. If only add the inner points alone, then get the two lines, but the outer one isn't traced as one presumes is the intended effect.
ADDENDUM:
The solid comes from using the same animated line handle for both...since the two calls alternate inner/outer positions, the lines are drawn back and forth between the two. They're close enough together to give the illusion of a surface which is, at first, confusing as to what one is seeing.
Not sure what the intended look really is, but
h=animatedline('Color','r');
k=animatedline('Color','r');
for i=1:length(t)
addpoints(h,x(i),y(i),z(i));
addpoints(k,0.5*x(i),0.5*y(i),z(i));
pause(0.01)
end
gives the two lines, one retracing original curve and the inner at half diameter.
  댓글 수: 5
Hassan Alkomy
Hassan Alkomy 2021년 1월 7일
Yes, I have tried it.
The problem is that it startes by ploting the the first subplot, then when the animation in the first plot ends, it starts drawing the second plot. This is becasue I have the second subplot after the end of the for loop. I have tried many ways to overcome this issue, but I was not able to find a way. I think it is something in the order of the command lines.
What I want is ploting the two subplots simaltenuously.
Here is an example of the code I used:
t=0:0.01:10;
x=cos(t);
y=sin(t);
z=t;
subplot(2,1,1)
plot(x,y)
hold on
h=animatedline('Color','r');
for i=1:length(t)
addpoints(h,x(i),y(i))
drawnow
end
subplot(2,1,2)
plot(x,y)
hold on
h=animatedline('Color','r');
for i=1:length(t)
addpoints(h,x(i),y(i))
drawnow
end
I think that somehow I have to create the two subplots of plot(x,y) simaltenuously first, then I need to plot the animatted lines simaltenuously, but I am struggling find a way to do that.
Hassan Alkomy
Hassan Alkomy 2021년 1월 7일
UPDATE
I found a way to do that. The main problem was trying to animate two lines with the same animatedline (for example h). Here is a working code
clear all;clc;close all;
t=0:.01:3;
x=cos(t);
y=sin(t);
z=t;
subplot(2,1,1)
plot(x,y)
hold on
h=animatedline('Color','r','LineStyle',':','LineWidth',3);
subplot(2,1,2)
plot(x,y)
hold on
k=animatedline('Color','r','LineStyle',':','LineWidth',3);
for i=1:length(t)
subplot(2,1,1)
addpoints(h,x(i),y(i));
subplot(2,1,2)
addpoints(k,x(i),y(i));
pause(.1)
end
The main point again is to have different names for each animated line even if they are the same, or at least this is how it worked with me.
Thank you @dpb for your helpful answers.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Animation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by