Using timer, How can I cleanly exit a function execution, when the timer fires in MATLAB?
22 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
taima zoabi
am 13 Dez. 2019
Kommentiert: Walter Roberson
am 3 Mär. 2021
My problem is that I can't find a Function to callback as a 'TimerFcn', that cleanly exits my function without errors.
t = timer('StartDelay',2,'TimerFcn', );
Thanks in advance.
2 Kommentare
Guillaume
am 13 Dez. 2019
Can you explain more clearly what you're trying to do exactly, particularly, the order of execution of the various things you want to happen.
Akzeptierte Antwort
Walter Roberson
am 13 Dez. 2019
Bearbeitet: Walter Roberson
am 21 Dez. 2019
There are only four ways in MATLAB to force a function to stop executing without its cooperation:
- quit MATLAB
- force an out-of-memory error
- force an infinite recursion
- Use jave robot or similar to simulate pressing control-C in the command line (note: this might not terminate immediately
There is no way to send a signal to a particular function to force that one function to stop, and there is no way to send a signal to a particular function to force that one function to error().
Therefore what you should do is write your timer to set a flag in an area that the other code checks periodically.
4 Kommentare
Walter Roberson
am 3 Mär. 2021
I just tried in R2020b, and found that MATLAB:pmaxsize (too much memory) and MATLAB:lang:StackOverflow (too many levels of recursion) can both be caught. However, it would be worth testing out what happens when the problem is triggered in a callback that is not really part of the code being executed.
Weitere Antworten (1)
Sean de Wolski
am 13 Dez. 2019
Why use a timer for this?
In the start of your function, start a tic
t = tic
Then periodically check
if toc(t)>2
return
end
1 Kommentar
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!