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

16 ビュー (過去 30 日間)
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?

採用された回答

Geoff Hayes
Geoff Hayes 2017 年 11 月 22 日
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.
  5 件のコメント
L
L 2021 年 10 月 22 日
Hello Geoff,
I saw your "myTimer" function, it works great.
Also, I have a question. Is it possible to add a stop time, or a number of cycles that should be executed in the "myTimer" object? Is there a field in "timer" for this?
For example, let the script run every minute for 1h, or let the script run every minute 10000 times?
Also, is it possible to start the script with this timer without F5(RUN) function, maybe with a special trigger?
Thank you.
Geoff Hayes
Geoff Hayes 2021 年 10 月 22 日
@L try changing the TasksToExecute field to indicate how many times you want the timer callback function to execute.
As for starting the script...you can call it from the command line as well but I'm not sure if that is what you are really looking for.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by