Continuous running of code

11 ビュー (過去 30 日間)
Alex Perkins
Alex Perkins 2015 年 3 月 20 日
編集済み: Walter Roberson 2018 年 6 月 4 日
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 件のコメント
Alex Perkins
Alex Perkins 2015 年 3 月 23 日
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 2015 年 3 月 24 日
Alex - yes, it is possible to do this. You should be able to modify the below code to suit your needs.

サインインしてコメントする。

回答 (2 件)

Geoff Hayes
Geoff Hayes 2015 年 3 月 21 日
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 2018 年 6 月 4 日
Is there any command in matlab to run the program for infinite time and update the data ?
  2 件のコメント
Walter Roberson
Walter Roberson 2018 年 6 月 4 日
編集済み: Walter Roberson 2018 年 6 月 4 日
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 2018 年 6 月 4 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by