How can I pause the execution of one timer while continuing to execute another timer in MATLAB 7.8 (R2009a)?

19 views (last 30 days)
I have created two timer objects and I want to pause one them while the other one continues executing its Timer Function callback. I have put a pause in the Timer Function callback for the first timer, but that stopped execution of both timers:
function timer_pause_test
t1 = timer('TasksToExecute', 2,'ExecutionMode','fixedRate','TimerFcn',@tfunc1);
t2 = timer('TasksToExecute', 2,'ExecutionMode','fixedRate','TimerFcn',@tfunc2);
%start the timers
start(t1)
start(t2)
function tfunc1(obj, event)
disp('Timer ONE is executing')
function tfunc2(obj, event)
disp('Timer TWO is executing')
pause(5)
I observe that the PAUSE statement in the second timer's (t2) callback pauses both timers' execution:
Timer ONE is executing
Timer TWO is executing
% both timers pause for 5 seconds
Timer ONE is executing
Timer TWO is executing
% both timers pause for 5 seconds

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 30 Jan 2017
The PAUSE statement in the timer's callback function will pause MATLAB execution completely. PAUSE will not stop the MATLAB execution if the PAUSE is outside the timer's TimerFcn callback.
To pause timer's execution,you can use timer's 'StartDelay' property. You can stop the timer at a certain point, set 'StartDelay' property of a timer, and then start the timer again. The timer's execution will start after 'StartDelay' amount of time elapses. Consider the following example:
function timer_test
t1 = timer('ExecutionMode','fixedRate','TimerFcn','disp(''Timer ONE is executing'')');
t2 = timer('ExecutionMode','fixedRate','TimerFcn','disp(''Timer TWO is executing'')');
% start both timers
start(t1)
start(t2)
% the timers continue executing despite the PAUSE statement
pause(3)
%stop the 1st timer
stop(t1)
disp('Timer 1 is stopped. Its execution will start in 3 seconds')
% set the timer's StartDelay property to introduce the pause in timer's execution
set(t1,'StartDelay',3)
%start the timer, it will start executing after 3 seconds have elapsed.
start(t1)
% continue running timers for 5 more seconds then stop the timers and delete them
pause(5)
stop(t1)
stop(t2)
delete(t1)
delete(t2)

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2009a

Community Treasure Hunt

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

Start Hunting!