Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
giacomo bruno
giacomo bruno am 22 Apr. 2016
Beantwortet: Swaraj am 9 Feb. 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 Kommentar
Adam Danz
Adam Danz am 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);

Melden Sie sich an, um zu kommentieren.

Antworten (1)

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

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by