Creating a Countdown Timer in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter amazingzuc
  • Start date Start date
  • Tags Tags
    Matlab Timer
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 11K views
amazingzuc
Messages
1
Reaction score
0
Im trying to make a countdown timer for my MATLAB class using the tic and toc functions.
so far this is what I have:

function lab4program1
disp('Welcome to the countdown timer program');
done = false;
tic();
while ~done
time = input('Enter a time (seconds) or negative number to quit:');

****************************

disp(toc);


last_time = toc();
while (true)
elapsed_time = toc()-last_time
last_time = last_time+elapsed_time;
end

if done
disp('BLAST OFF!');
end

end


Can anyone help me with the loop that goes where the asterisks are?
 
Physics news on Phys.org
Hi what about this

function labtimer
disp('Welcome to the countdown timer program');time = input('Enter a time (seconds) ');
tstart=tic; % store starting time
while time >1 % if counter is greater than 1

if (abs(toc(tstart)-1))<0.01 % if one second has elapsed

tstart=tic; % start timer again for next second
time=time-1; % reduce the time by one as one second has elapsed
disp(time) % display the counter
end

end
end