Borrar filtros
Borrar filtros

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

3 visualizaciones (últimos 30 días)
giacomo bruno
giacomo bruno el 22 de Abr. de 2016
Respondida: Swaraj el 9 de Feb. de 2023
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 comentario
Adam Danz
Adam Danz el 14 de Sept. de 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);

Iniciar sesión para comentar.

Respuestas (1)

Swaraj
Swaraj el 9 de Feb. de 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

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by