Continuous running of code
조회 수: 9 (최근 30일)
이전 댓글 표시
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
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
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!
댓글 수: 0
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
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
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 Exchange에서 Graphics Performance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!