Animating a matlab calculated data over a given data in real time.

5 vues (au cours des 30 derniers jours)
manikant sinha
manikant sinha le 1 Fév 2024
Commenté : manikant sinha le 1 Fév 2024
Hi,
I have simulated data of two different parameters y1 and y2 against variable x which I have plotted in Matlab using subplot function.
Now, I am calculating y1 and y2 using some other technique in Matlab for all x points using loop and I want that as soon as y1 and y2 are calculated for a given x, it should get updated in their respective subplots.
How to do that in Mtalab?
I know "drawnow" and "animatedline" command in Matlab but unable to fix my problem.

Réponse acceptée

Gyan Vaibhav
Gyan Vaibhav le 1 Fév 2024
Modifié(e) : Gyan Vaibhav le 1 Fév 2024
Hi Manikant,
For your query, you will need to initialize an animateline object for each subplot and keep adding points to each object using the addpoints method. To update these on to the figure you can use the drawnow.
Here is a sample code:
x = linspace(0, 10, 100);
figure;
% Create two subplots
subplot(2,1,1);
xlabel('x');
ylabel('y1');
hLine1 = animatedline;
axis([min(x)-1 max(x)+1 -1 1])
subplot(2,1,2);
xlabel('x');
ylabel('y2');
hLine2 = animatedline;
axis([min(x)-1 max(x)+1 -1 1])
for i = 1:length(x)
y1(i) = sin(x(i));
y2(i) = cos(x(i));
% Add points to animated lines
addpoints(hLine1, x(i), y1(i));
addpoints(hLine2, x(i), y2(i));
% Update the plots
drawnow %limitrate
% Optional:
%pause(0.1);
end
Few quick tips:
  1. You can set the limits for your x and y values, so as the animation looks nice.
  2. You can use the pause function or "limitrate" with the drawnow function, so as the plot updates slowly.
Hope this helps.
Thanks
Gyan
  1 commentaire
manikant sinha
manikant sinha le 1 Fév 2024
Thanks Gyan for you kind help.
Meanwhile, I also tried this code and it fits my purpose.
clc;
clear;
close all;
x = linspace(0,4*pi,1000);
subplot(2,1,1)
y1 = cos(x);
plot(x,y1);
h1 = animatedline;
subplot(2,1,2)
y2 = sin(x);
plot(x,y2);
h2 = animatedline;
y3 = zeros(1000,1);
y4 = zeros(1000,1);
for k = 1:length(x)
y3(k) = cos(x(k));
addpoints(h1,x(k),y3(k));
drawnow
y4(k) = sin(x(k));
addpoints(h2,x(k),y4(k));
drawnow
end
%

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by