Continuous running of code

24 views (last 30 days)
Alex Perkins
Alex Perkins on 20 Mar 2015
Edited: Walter Roberson on 4 Jun 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 Comments
Alex Perkins
Alex Perkins on 23 Mar 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 on 24 Mar 2015
Alex - yes, it is possible to do this. You should be able to modify the below code to suit your needs.

Sign in to comment.

Answers (2)

Geoff Hayes
Geoff Hayes on 21 Mar 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 on 4 Jun 2018
Is there any command in matlab to run the program for infinite time and update the data ?
  2 Comments
Walter Roberson
Walter Roberson on 4 Jun 2018
Edited: Walter Roberson on 4 Jun 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 on 4 Jun 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.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!