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?

Réponses (3)

Walter Roberson
Walter Roberson le 17 Juin 2018

0 votes

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

13 commentaires

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 le 17 Juin 2018
Thank you so much Walter. It worked.
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'
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 le 20 Sep 2019
Modifié(e) : Nikolaos Zafirakis le 20 Sep 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.
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
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));
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
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));
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 le 20 Sep 2019
Thank you, for you help. If you could check this out to it would be helpful!
Noah Prisament
Noah Prisament le 7 Juin 2023
Modifié(e) : Noah Prisament le 7 Juin 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 le 7 Juin 2023

Connectez-vous pour commenter.

Abhibrata Adhikary
Abhibrata Adhikary le 17 Juin 2018

0 votes

Can you give an example to show the working?
Hussein
Hussein le 8 Juil 2023

0 votes

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

Catégories

En savoir plus sur Animation dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by