I want to take a 2-dimensional vector and plot it, but I want to show the line being drawn as if the data is being gathered in real time. However, I'll have the data already stored in an array.
For example, if I wanted to plot y = sin(x) from x = 1 to 25, then I would use the following command:
plot(sin(1:0.01:25));
But I want to see the curve being drawn as if it's being done in real-time. How would I do this?

 Respuesta aceptada

the cyclist
the cyclist el 25 de Jul. de 2013

3 votos

One possible way:
x = 1:0.01:25;
y = sin(x);
n = numel(x);
figure
hold on
for i = 1:n
plot(x(1:i),y(1:i))
xlim([0 25])
ylim([-1.1 1.1])
pause(0.05)
end

4 comentarios

Caleb
Caleb el 25 de Jul. de 2013
I had nearly this same code, but I was putting xlim and ylim outside of the loop. Thank you!
the cyclist
the cyclist el 25 de Jul. de 2013
It is actually better outside the loop, as long as it is before the plotting. So, this is slightly more efficient:
x = 1:0.01:25;
y = sin(x);
n = numel(x);
figure
xlim([0 25])
ylim([-1.1 1.1])
hold on
for i = 1:n
plot(x(1:i),y(1:i))
pause(0.05)
end
Alireza Madani
Alireza Madani el 30 de Abr. de 2022
Does anyone know how can we program this scenario in a way that the speed of the while/for loop is controller with the real time? For example if we want to plot y = sin(t) and t = 0 : 0.01 : 25. It actually takes 25 seconds for the MATLAB to plot this.
Khang Nguyen
Khang Nguyen el 7 de Sept. de 2023
Hi, can you graph multiple plots live at the same time?

Iniciar sesión para comentar.

Más respuestas (1)

Anshul Rathore
Anshul Rathore el 25 de Mzo. de 2019

11 votos

Don't use hold on instead use drawnow function to update your plots as it is much faster.
x = 1:0.01:25;
y = sin(x);
n = numel(x);
figure;
for i = 1:n
plot(x(1:i),y(1:i),'-r');
xlim([0 25]);
ylim([-1.1 1.1]);
drawnow;
end

2 comentarios

Abdoo
Abdoo el 12 de Feb. de 2021
Editada: Abdoo el 12 de Feb. de 2021
could you show me, how I can plot on real time reading from usb?
Jonathan Arias
Jonathan Arias el 22 de Mayo de 2021
you have to add this line (before drawnow):
addpoints(an,x(i),y(i));
More examples: https://la.mathworks.com/help/matlab/ref/drawnow.html

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 25 de Jul. de 2013

Comentada:

el 7 de Sept. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by