MATLAB Answers

How to stop cyclic program in Matlab online - timer

54 views (last 30 days)
Jan Skach
Jan Skach on 29 Mar 2020
Answered: Steven Lord on 28 Oct 2020
Hello,
could you please help me with the following issue?
I tried to run cycling program in online Matlab (https://matlab.mathworks.com/) in order to try this functionality - my instention is to write a script that will continuously during a day mine some data). I wrote the following commands in a script and run it. Wince then I have a cyclic response in command window and I don't know how to stop the execution.
M-file contained the following: - now I know that the function "start" was not a really good name to choose.
function start ()
fileID = fopen('exptable.txt','w');
a = timer;
set(a,'ExecutionMode','FixedRate');
set(a,'TimerFcn','@() iwrite()');
start (a);
function iwrite()
b = rand;
fprintf(fileID,'%f \n',b);
end
end
This is every second written in the command window:
ans =
function_handle with value:
@()iwrite()

  0 Comments

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 29 Mar 2020
It appear you don't have the handle to the timer object. You can get that handle using timerfind, but I recommend running this first to delete all the timers currently created.
delete(timerfind)

  1 Comment

Jan Skach
Jan Skach on 30 Mar 2020
Hello Ameer,
thank you for the hint. It seems that the cyclic process was stopped over the night. However, to be sure I run your command. No timer exists anymore.
Jan

Sign in to comment.

More Answers (2)

Cris LaPierre
Cris LaPierre on 7 May 2020
Try using the stop button in the bottom right corner of your MATLAB window.

  1 Comment

Hermes Suen
Hermes Suen on 28 Oct 2020
This button no longer seems to be popping up on MATLAB R2020b, Chrome

Sign in to comment.


Steven Lord
Steven Lord on 28 Oct 2020
Let's go through your function line by line.
function start ()
fileID = fopen('exptable.txt','w');
a = timer;
set(a,'ExecutionMode','FixedRate');
Looks good to this point.
set(a,'TimerFcn','@() iwrite()');
This previous line doesn't do what you think it does. MATLAB will run this code to create an anonymous function with body iwrite(), assign that anonymous function to ans, and display ans. [Calling that anonymous function will not call the iwrite local function, as that is not in scope.]
To have MATLAB call the function handle to the iwrite function that was set as the TimerFcn when this line of code was executed:
set(a, 'TimerFcn', @(~, ~) iwrite())
You don't use the timer object handle or the event data that the timer will pass into the TimerFcn inside that TimerFcn, so I just ignore them.
start (a);
function iwrite()
b = rand;
fprintf(fileID,'%f \n',b);
end
end
The rest of your code is fine, except that you leave the file open even after you stop the timer. You probably want to set the timer's StopFcn inside your start function and have it close the file when it is executed. Again, since the StopFcn doesn't need to do anything with the timer object or the event data, I use ~ to ignore what MATLAB passes into the StopFcn automatically.
a.StopFcn = @(~, ~) fclose(fileID);
I'd also return the timer from start, so that it's easy to stop.
function a = start ()
Call this as:
T = start();
% wait a little while
stop(T)
edit exptable.txt

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by