I am trying to print a warning or just a message after every second, like "1 second elapsed". Is there a possibility to realize that?
I tried it with tic toc and a loop, but it is pretty insufficient. Furthermore I never get exactly one second. Is there a command that prints every X ms automatically? Any ideas?
Thanks in advance.
As @Daniel suggested, timer objects are the way to go.
One thing to remember is that MATLAB timers execute asynchronously but are not truly parallel (even though timer objects run in a different thread, the MATLAB interpreter is still single-threaded).
So timer events might not fire (
BusyMode
property defaults todrop
) it they occur during certain lengthy non-interruptible operations (like builtin functions, MEX-functions, etc..).Here is an example:
Note how the timer event did not get a chance to execute while the
EIG
function was running.Now if we change the
BusyMode
property toqueue
, then once again the event will not fire during the execution ofEIG
operation, but will immediately spit out all the queued events onceEIG
finishes:Use a timer object.
It is called
pause
. The optional argument specifies the duration in seconds. Documentation: http://www.mathworks.de/de/help/matlab/ref/pause.html