Continuous running of code

13 visualizaciones (últimos 30 días)
Alex Perkins
Alex Perkins el 20 de Mzo. de 2015
Editada: Walter Roberson el 4 de Jun. de 2018
I have been trying to implement a way to allow my code to run continuously on a MATLAB graph but with no avail. I have used the drawnow function which has helped me run it in real-time but I would like for the code to run like oscilloscope. Is there a function that I am missing?
  3 comentarios
Alex Perkins
Alex Perkins el 23 de Mzo. de 2015
The following code worked well in the sense that it is updating but I have an equation in a for loop for 0.7 seconds and I have it now to update every 0.7 seconds but instead of the plot continuing from the previous one it essentially pasted the same info. Is it possible to do this?
Geoff Hayes
Geoff Hayes el 24 de Mzo. de 2015
Alex - yes, it is possible to do this. You should be able to modify the below code to suit your needs.

Iniciar sesión para comentar.

Respuestas (2)

Geoff Hayes
Geoff Hayes el 21 de Mzo. de 2015
Alex - you could try the following which will generate a sine wave and change the last second of data on each iteration of the while loop
figure;
hold on;
set(gca,'Color','k');
set(gca,'YLim',[-2 2]);
% generate five seconds of data with frequency f
fs = 200;
dt = 1/fs;
t = linspace(0,5-dt,5*fs);
f = 2;
a = 1;
y = a*sin(2*pi*t*f);
h = plot(t,y,'Color','g');
while true
% generate a new second of data
tn = linspace(max(t)+dt,max(t)+1,fs);
% randomly change the frequency
if rand>0.6
fn = randi(5,1,1);
else
fn = f;
end
yn = sin(2*pi*tn*fn);
% remove the old data and replace with the new
t = [t(fs+1:end) tn];
y = [y(fs+1:end) yn];
% update the plot
set(h,'XData',t,'YData',y);
pause(1);
end
Note how set is used to update the x and y data of the handle h which corresponds to plot graphics handle. Try the above and see what happens!

Shubham Hokarne
Shubham Hokarne el 4 de Jun. de 2018
Is there any command in matlab to run the program for infinite time and update the data ?
  2 comentarios
Walter Roberson
Walter Roberson el 4 de Jun. de 2018
Editada: Walter Roberson el 4 de Jun. de 2018
No, MATLAB does not have any transfinite operations, but you might be able to find some useful information about implementation of transfinite quantities in MATLAB from https://www.maths.ox.ac.uk/system/files/attachments/PartB2015-16_web_0.pdf
I think you will find it easier to execute indefinitely, updating as you go, instead of waiting until after an infinite time has passed.
Geoff Hayes
Geoff Hayes el 4 de Jun. de 2018
Shubham - which program and which data (should be updated)? Please provide some context. And in the future, please don't post a question as an answer to a different question.

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Performance en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by