2D plot and animation

figure();
plot(x(:,1),x(:,3),'b',x(:,5),x(:,7),'r',x(:,9),x(:,11),'g')
axis([0 15 0 15])
I want to make a animation to plot the graph as shown. i.e it should start with initial point and slowly goes giving me an animation. The normal graph is coming but I want it in the form of animation. Can someone please help me in this?

Respuestas (3)

Walter Roberson
Walter Roberson el 17 de Jun. de 2018

0 votos

I recommend using animatedLine()
But you could also consider comet()

13 comentarios

Walter Roberson
Walter Roberson el 17 de Jun. de 2018
L1 = animatedline('color', 'b');
L2 = animatedline('color', 'r');
L3 = animatedline('color', 'g');
for K = 1 : size(x,1)
addpoints(L1, x(K,1), x(K,3) );
addpoints(L2, x(K,5), x(K,7) );
addpoints(L3, x(K,9), x(K,11) );
drawnow update;
end
Abhibrata Adhikary
Abhibrata Adhikary el 17 de Jun. de 2018
Thank you so much Walter. It worked.
Nikolaos Zafirakis
Nikolaos Zafirakis el 20 de Sept. de 2019
What can you do if x is a time unit?
x =
'23-Jun-2017 08:34:18'
'23-Jun-2017 08:34:29'
'23-Jun-2017 08:34:39'
Walter Roberson
Walter Roberson el 20 de Sept. de 2019
For the last few years you have been able to convert time units to datetime() objects and plot them directly, such as
xdt = datetime(x);
plot(xdt, y)
Nikolaos Zafirakis
Nikolaos Zafirakis el 20 de Sept. de 2019
Editada: Nikolaos Zafirakis el 20 de Sept. de 2019
Yes my data is already in that format what if:
L1 = animatedline('color', 'b');
L2 = animatedline('color', 'r');
L3 = animatedline('color', 'g');
xdt = datetime(x);
for K = 1 : size(x,1)
addpoints(L1, xdt(K,1), x(K,1) );
addpoints(L2, xdt(K,1), x(K,2) );
addpoints(L3, xdt(K,1), x(K,3) );
drawnow update;
end
% Error using matlab.graphics.animation.AnimatedLine/addpoints
% Invalid type for argument X. Type must be double.
Walter Roberson
Walter Roberson el 20 de Sept. de 2019
I see what you mean, animatedLine is restricted to double.
But you also have the problem that x is still a cell array of character vectors, so plotting x(K,1) as the y coordinate is not possible.
Below, I will assume that you have a numeric matrix Y(K,1:3) [though it could also be datetime or duration or categorical instead of numeric.]
In that case you need older techniques:
L1 = plot(nan, nan, 'b');
L2 = plot(nan, nan, 'r');
L3 = plot(nan, nan, 'g');
xdt = datetime(x);
for K = 1 : size(xdt,1)
cxdt = xdt(1:K);
set(L1, 'XData', cxdt, 'YData', Y(1:K,1));
set(L2, 'XData', cxdt, 'YData', Y(1:K,2));
set(L3, 'XData', cxdt, 'YData', Y(1:K,3));
drawnow limit;
end
Nikolaos Zafirakis
Nikolaos Zafirakis el 20 de Sept. de 2019
So I tried it and i got this error. Is there a was around this?
Error using matlab.graphics.chart.primitive.Line/set
Invalid or deleted object.
Error in Untitled (line 35)
set(L1, 'XData', cxdt, 'YData', Y(1:K,1));
Walter Roberson
Walter Roberson el 20 de Sept. de 2019
L1 = plot(nan, nan, 'b');
hold on
L2 = plot(nan, nan, 'r');
L3 = plot(nan, nan, 'g');
hold off
xdt = datetime(x);
for K = 1 : size(xdt,1)
cxdt = xdt(1:K);
set(L1, 'XData', cxdt, 'YData', Y(1:K,1));
set(L2, 'XData', cxdt, 'YData', Y(1:K,2));
set(L3, 'XData', cxdt, 'YData', Y(1:K,3));
drawnow limit;
end
Nikolaos Zafirakis
Nikolaos Zafirakis el 20 de Sept. de 2019
Well the datetime does not work, it errors so I changed the time unit to Julian date, and it works. However, I would prefer it to work with the UTC data and time. If you have any further suggestions, please let me know. Thank you for your help!!
Error using matlab.graphics.chart.primitive.Line/set
Value must be a vector of numeric type
Error in Untitled (line 35)
set(L1, 'XData', cxdt, 'YData', Y(1:K,1));
Walter Roberson
Walter Roberson el 20 de Sept. de 2019
L1 = plot(NaT, nan, 'b');
hold on
L2 = plot(NaT, nan, 'r');
L3 = plot(NaT, nan, 'g');
hold off
xdt = datetime(x);
for K = 1 : size(xdt,1)
cxdt = xdt(1:K);
set(L1, 'XData', cxdt, 'YData', Y(1:K,1));
set(L2, 'XData', cxdt, 'YData', Y(1:K,2));
set(L3, 'XData', cxdt, 'YData', Y(1:K,3));
drawnow limit;
end
Nikolaos Zafirakis
Nikolaos Zafirakis el 20 de Sept. de 2019
Thank you, for you help. If you could check this out to it would be helpful!
Noah Prisament
Noah Prisament el 7 de Jun. de 2023
Editada: Noah Prisament el 7 de Jun. de 2023
The "animatedline" now supports "datetime" values natively, so this functionality can now be acheived using "animatedline" and "addpoints" if the AnimatedLines are initialized as follows:
L1 = animatedline(NaT, NaN, 'color', 'b');
L2 = animatedline(NaT, NaN, 'color', 'r');
L3 = animatedline(NaT, NaN, 'color', 'g');
Walter Roberson
Walter Roberson el 7 de Jun. de 2023

Iniciar sesión para comentar.

Abhibrata Adhikary
Abhibrata Adhikary el 17 de Jun. de 2018

0 votos

Can you give an example to show the working?
Hussein
Hussein el 8 de Jul. de 2023

0 votos

clc clear all close all Z = peaks; surf(Z) axis tight set(gca,'nextplot','replacechildren','visible','off') f = getframe; [im,map] = rgb2ind(f.cdata,256,'nodither'); im(1,1,1,20) = 0; for k = 1:20 surf(cos(2*pi*k/20)*Z,Z) f = getframe; im(:,:,1,k) = rgb2ind(f.cdata,map,'nodither'); end imwrite(im,map,'DancingPeaks.gif','DelayTime',0.1,'LoopCount',inf) %g443800

Categorías

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

Productos

Preguntada:

el 17 de Jun. de 2018

Respondida:

el 8 de Jul. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by