Azzera filtri
Azzera filtri

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

3 visualizzazioni (ultimi 30 giorni)
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 Commento
Adam Danz
Adam Danz il 14 Set 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);

Accedi per commentare.

Risposte (1)

Swaraj
Swaraj il 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

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by