Clear Filters
Clear Filters

Can i get the remaining time from the start of a timer?

3 views (last 30 days)
Hi everyone, I was wondering if it was possible to get the amount of time that remains before a timer executes its TimerFcn? I have a very simple timer such as:
t = timer;
t.StartDelay = 300;
t.TimerFcn = @(myTimerObj, thisEvent)disp('300 seconds have elapsed');
start(t)
How can I know how many more seconds I have before the text appears? Thank you in advance
  1 Comment
Adam Danz
Adam Danz on 14 Sep 2019
You can approximate time remaining by using tic/toc.
t = timer;
t.StartDelay = 300;
t.TimerFcn = @(myTimerObj, thisEvent)disp('300 seconds have elapsed');
t0 = tic();
start(t)
% to check time remaining
remainingTime = t.StartDelay-toc(t0);

Sign in to comment.

Answers (1)

Swaraj
Swaraj on 9 Feb 2023
You can use tic, toc and a while loop for this.
t = timer;
t.StartDelay = 20;
t.TimerFcn = @(myTimerObj, thisEvent)disp('20 seconds have elapsed');
sTime = tic;
start(t)
while (toc(sTime) < t.StartDelay)
fprintf('%.0f seconds remaining\n', t.StartDelay - toc(sTime));
pause(1);
end

Categories

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

Community Treasure Hunt

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

Start Hunting!