How to stop cyclic program in Matlab online - timer
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jan Skach
le 29 Mar 2020
Réponse apportée : Steven Lord
le 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 commentaires
Réponse acceptée
Ameer Hamza
le 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)
Plus de réponses (2)
Cris LaPierre
le 7 Mai 2020
Try using the stop button in the bottom right corner of your MATLAB window.
1 commentaire
Steven Lord
le 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 commentaires
Communautés
Plus de réponses dans Distance Learning Community
Voir également
Catégories
En savoir plus sur Platform and License dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!