How can I change the values of a plot inside a loop?

9 vues (au cours des 30 derniers jours)
Lobo
Lobo le 11 Déc 2013
Réponse apportée : Lobo le 11 Déc 2013
Hi all,
I want to plot something and then change the values of some variables inside a loop without plotting everything again. I think something as 'handle' and 'set' would help but, honestly, I do no have any idea of how to use it. I am going to use real-time data, so I want to use quick functions, this is the reason because I don't want to plot again and just change the values inside a plot already defined. Something like that:
h=plot(x,y)
n=0;
while n<5
x=x+n;
y=y-n;
%%change the values of x and y in the previous plot%%
n=n+1;
end

Réponse acceptée

Kelly Kearney
Kelly Kearney le 11 Déc 2013
You can set the 'xdata' and 'ydata' properties of the line. You may have to make sure you set your axis limits so they aren't reset as the data moves.
x = sort(rand(10,1));
y = rand(10,1);
h = plot(x,y);
set(gca, 'xlim', [0 20], 'ylim', [-10 1]);
n=0;
while n<5
x=x+n;
y=y-n;
n=n+1;
set(h, 'xdata', x, 'ydata', y);
pause(1);
end

Plus de réponses (1)

Lobo
Lobo le 11 Déc 2013
That is exactly was I was looking for. Thank you very much!

Catégories

En savoir plus sur 2-D and 3-D Plots 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