Timers in matlab can spawn a seperate thread to wait in which can get around the problem of needing to look from outside the current matlab instance.
We can set the timer to check if debug mode is active and if it is to do something.
A example function to check if debug mode is active and if so to do something:
function mycallbackfunction(~,~)
if feature('IsDebugMode') % undocumented thanks to CatzLoveJazz
load handel
sound(y,Fs)
The two previous lines are an attention grabbing example, other possibilities are to use beep
, to write to a file, or run any commands or function.
evalin('base','stop(timerHandle)') % stop the timer
end
end
This function could be modified to evaluate the 'attention grab' once and then reset once debug mode is no longer active. Currently it relies on just stopping and then manually restarting the timer.
(note: a previous version had an else however this was redundant as it will not run while the workspace is busy)
Now to create the timer object.
timerTic=4; % how often the timer checks
timerHandle = timer();
timerHandle.startDelay = timerTic;
timerHandle.Period = timerTic;
timerHandle.ExecutionMode = 'fixedRate';
timerHandle.TasksToExecute = inf;
timerHandle.TimerFcn = @mycallbackfunction;
and to start to timer call
start(timerHandle)
The timer will automatically stop after running the attention grabbing lines.
If debug mode is never entered the timer will keep running and will need to be stoped manually with stop(timerHandle)
Remember to run delete(timerHandle)
once finished to remove the object before clearing the timerHandle
variable