how can i animate the line using for loop and if statement

clear all
close all
clc
h = animatedline;
axis([1 4 1 4])
for x=1:4
if x==1
y(x)=1;
elseif x==2
y(x)=1;
elseif x==3
y(x)=2;
elseif x==4
y(x)=1;
elseif x>4
break;
addpoints(h,x(x),y(x));
drawnow
x=1:4;
end
end
I want the line should be move slowly through these points, please help me to fix this.

 Respuesta aceptada

hello
you can do much simpler , as x and y are fully defined in the initialization section , then you only have to loop - four times and that's it ! no need for if elseif statements
clear all
close all
clc
figure
h = animatedline;
axis([0 5 0 5])
x=1:4;
y = [1 1 2 1];
for ci=1:4
addpoints(h,x(ci),y(ci));
pause(0.5);
drawnow
end
.

7 comentarios

thank you so much!! now i want that line moves smoothier, how can i do that?? and marker leds the line will be fine.
something like that ?
clear all
close all
clc
figure
h = animatedline;
h.Marker = '*';
axis([0 5 0 5])
x=1:4;
n = 100;
xx=linspace(x(1),x(end),n);
y = [1 1 2 1];
yy = interp1(x,y,xx);
for ci=1:n
addpoints(h,xx(ci),yy(ci));
pause(0.1);
drawnow
end
nice, now the line is smooth
can you please explain this??
n = 100;
xx=linspace(x(1),x(end),n);
y = [1 1 2 1];
yy = interp1(x,y,xx);
one last question please
if i have an another 4 data points which is x=1:4, y=[2 2 3 2] now, i want to create another animated line in same plot, how can i do this??
to the first question, I repalced the iteration from only 4 steps to n = 100 steps.
so this will give you the more progressive dispaly with 100 points per line and not only 4
to do so, I interpolated from the original y data to 100 evenly spaced x values
to the second question, simply do the same for a second animatedline object :
clear all
close all
clc
figure
h1 = animatedline;
h1.Marker = '*';
h2 = animatedline;
h2.Marker = '+';
axis([0 5 0 5])
x1=1:4;
n = 100;
xx1=linspace(x1(1),x1(end),n);
y1 = [1 1 2 1];
y2 = [2 2 3 2];
yy1 = interp1(x1,y1,xx1);
yy2 = interp1(x1,y2,xx1);
for ci=1:n
addpoints(h1,xx1(ci),yy1(ci));
addpoints(h2,xx1(ci),yy2(ci));
pause(0.1);
drawnow
end
Thank you! how can i change the colors of these lines except red,blue, green
for example i want orange and dark green or some other colors. how can i do this??
hello
see
code updated for orange and green colors
clear all
close all
clc
figure
h1 = animatedline;
h1.Marker = '*';
h1.Color = [1 0.4 0]; % orange
h2 = animatedline;
h2.Marker = '+';
h2.Color = [0 1 0]; % green
axis([0 5 0 5])
x1=1:4;
n = 100;
xx1=linspace(x1(1),x1(end),n);
y1 = [1 1 2 1];
y2 = [2 2 3 2];
yy1 = interp1(x1,y1,xx1);
yy2 = interp1(x1,y2,xx1);
for ci=1:n
addpoints(h1,xx1(ci),yy1(ci));
addpoints(h2,xx1(ci),yy2(ci));
pause(0.1);
drawnow
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Animation en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by