MATLAB Answers

0

I have a script that I run via a timer every 30 minutes -- how do I run this in background?

Asked by Robert Henson on 22 Nov 2017
Latest activity Commented on by Geoff Hayes
on 23 Sep 2019
My script runs on a timer every 30 minutes, and I realize I need to run this in background so I can continue to use Matlab for other work. How do I run this timer script in background?

  0 Comments

Sign in to comment.

1 Answer

Answer by Geoff Hayes
on 22 Nov 2017
 Accepted Answer

Robert - try creating a function that you can call from the command line. It will instantiate a timer and then periodically (whenever the timer callback fires) will do some work. For example, in a file named timerInBackground.m you could have
function [myTimer] = timerInBackground
myTimer = timer('Name','MyTimer', ...
'Period',30*60, ...
'StartDelay',0, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',@myTimerCallback);
start(myTimer);
function myTimerCallback(hObject, eventdata)
fprintf('hello!\n');
Then call this function from your command window as
myTimer = timerInBackground;
Then every 30 minutes (30*60 seconds) the timer callback will fire and (in this case) print a hello! message to the console window. In the meantime, you can continue with other MATLAB tasks.

  3 Comments

I tried this timer in background. However, I cannot stop it. How can I stop it?
Hsinho - where is the timer created? At the command line or from within some code? In either case, you could probably use timerfind to find (all) running timers and then stop them. Something like
myTimers = timerfind;
stop(myTimers);
delete(myTimers);

Sign in to comment.