Plot a normal figure and animatedline together onthe same figure

16 views (last 30 days)
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

Accepted Answer

dpb
dpb on 7 Jan 2021
Edited: dpb on 8 Jan 2021
...
% 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 Comments
Hassan Alkomy
Hassan Alkomy on 7 Jan 2021
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 on 7 Jan 2021
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.

Sign in to comment.

More Answers (0)

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!