How can i draw two animated plots on each subplots at the same time?

72 vues (au cours des 30 derniers jours)
Le Dung le 15 Déc 2017
Hi everyone! Now, i want to draw two animated plots on each subplots at the same time. You can see my code below. But, when i use this code, of course, Matlab will return two subplots and it will draw "k" line after drew "r" line. So, how can i do to draw two animated plots (in this case is two lines) at the same time? Thank you so much. My code:
x1=linspace(0,20,201);
y1=sin(x1);
x2=linspace(0,20,201);
y2=cos(x2);
sub1=subplot(2,1,1);
hold on
axis([0 21 -1.1 1.1]);
ani1=animatedline('Color','r');
for i=1:length(x1)
drawnow
pause(0.01);
end
subplot(2,1,2)
hold on
axis([0 21 -1.1 1.1]);
ani2=animatedline('Color','k');
for j=1:length(x1)
drawnow
pause(0.01);
end
0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Réponse acceptée

Jos (10584) le 15 Déc 2017
Merge the for-loops! This works here because x1,y1, x2, and y2 are all the same length. There is also no need for hold on)
x1=linspace(0,20,201);
y1=sin(x1);
x2=linspace(0,20,201);
y2=cos(x2);
sub1=subplot(2,1,1);
axis([0 21 -1.1 1.1]);
ani1=animatedline('Color','r');
subplot(2,1,2)
axis([0 21 -1.1 1.1]);
ani2=animatedline('Color','k');
for k=1:length(x1)
addpoints(ani2,x2(k),y2(k)) ;% I assume you meant to plot (x2,y2)
drawnow
pause(0.01);
end
3 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Jos (10584) le 15 Déc 2017
I assume there is a relationship between x1 and x2? For instance, both are time points. You can use interp1 to get, for instance, y2 values for each point in x1, like this:
x1 = linspace(0,20,200)
y1 = sin(x1) ;
x2 = linspace(0,20,100)
y2 = cos(x2) ;
y2_int = interp1(x2,y2,x1) ;
and now use x1 as the x coordinate for both animations.
Le Dung le 15 Déc 2017
It is very kind of you. Thank you so much. And, you can help me to my question on this link: https://www.mathworks.com/matlabcentral/answers/373112-how-can-i-specify-which-subplot-that-i-want-to-plot-on-it

Connectez-vous pour commenter.

Plus de réponses (1)

Riccardo Rosati le 4 Juin 2018
If I create the number of subplot iteratively (because this is for a GUI where I manually insert the number of signals that I want to plot), how can I do this? I should generate iteratively also the animated lines, but how?
0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

Catégories

En savoir plus sur Animation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by