MATLAB Answers

Plot a normal figure and animatedline together onthe same figure

18 views (last 30 days)
Hassan Alkomy
Hassan Alkomy on 6 Jan 2021
Edited: dpb on 8 Jan 2021
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
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)

Community Treasure Hunt

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

Start Hunting!