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?

답변 (3개)

Walter Roberson
Walter Roberson 2018년 6월 17일

0 개 추천

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

댓글 수: 13

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 2018년 6월 17일
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 2019년 9월 20일
편집: Nikolaos Zafirakis 2019년 9월 20일
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 2019년 9월 20일
Thank you, for you help. If you could check this out to it would be helpful!
Noah Prisament
Noah Prisament 2023년 6월 7일
편집: Noah Prisament 2023년 6월 7일
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 2023년 6월 7일

댓글을 달려면 로그인하십시오.

Abhibrata Adhikary
Abhibrata Adhikary 2018년 6월 17일

0 개 추천

Can you give an example to show the working?
Hussein
Hussein 2023년 7월 8일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 Animation에 대해 자세히 알아보기

제품

질문:

2018년 6월 17일

답변:

2023년 7월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by