How to stop cyclic program in Matlab online - timer
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jan Skach
el 29 de Mzo. de 2020
Respondida: Steven Lord
el 28 de Oct. de 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 comentarios
Respuesta aceptada
Ameer Hamza
el 29 de Mzo. de 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)
Más respuestas (2)
Cris LaPierre
el 7 de Mayo de 2020
Try using the stop button in the bottom right corner of your MATLAB window.

1 comentario
Hermes Suen
el 28 de Oct. de 2020
This button no longer seems to be popping up on MATLAB R2020b, Chrome

Steven Lord
el 28 de Oct. de 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 comentarios
Comunidades de usuarios
Más respuestas en Distance Learning Community
Ver también
Categorías
Más información sobre Startup and Shutdown en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!