Creating Countdown timer in Matlab App

Hi all,
I am trying to create a countdown timer in matlab. The user will press a button and that time will start to decrease. I have used an edit field text for the timer display. I am able to get the starting time displayed on the display, but am really struggling (i actually have no idea - even though I tried searching around) to determine how the 'countdown' works in Matlab. I have the following code:
properties (Access = private)
timerLeft = 0; % Description
end
and then:
function BeginButtonPushed(app, event)
end
function FunctionalityTest10sButtonPushed(app, event)
timerVal = 10;
app.timerLeft = timerVal;
app.timerCountdown.Value = '00:00:10';
end
All help is really appriciated.

Answers (1)

See attached demo. It's in GUIDE but you can click the link/button to convert it to App Designer.

5 Comments

Hey, firstly thanks for this, however, I do not understand what it means by clicking on the button to convert it to App Designer? I clicked both of the links you attached yet got an error messaage.
Right click in your browser and them download those files to your local drive. Then open guide
>> guide
When guide opens up, open up gostop.fig.
At the bottom there will be a banner asking if you want to convert the code to App Designer. Click that.
Hey, thanks for this. I got the 'timer' bit working with the pause command. However, what I don't know is how to get the display of hours, minutes and seconds in the format 00:00:00. I would be able to do it with just seconds with if statements and num2str but I have no clue on the minutes and hours bit aswell, and decreasing minutes as seconds go 00 etc. Could you help me with that. My current code is as follows:
function BeginButtonPushed(app, event)
message = '';
i = 1;
while i < app.timerLeft
app.timerLeft = app.timerLeft - 1;
pause(1);
fprintf(num2str(app.timerLeft));
end
end
% Button pushed function: TutorFunctionalityTest10sButton
function TutorFunctionalityTest10sButtonPushed(app, event)
timerVal = 10;
app.timerLeft = timerVal;
app.timerCountdown.Value = '00:00:10';
end
end
Examples:
dt = datestr(now, "HH:mm:ss")
dt = '13:04:54'
t = datetime('now','TimeZone','local','Format','d-MMM-y HH:mm:ss Z')
t = datetime
23-Apr-2022 13:34:55 +0000
t1 = datetime(2022,4,23,9,034,06, 'Format','HH:mm:ss')
t1 = datetime
09:34:06
Hey,
So I went about this in a similar but different manner using the duration command(I bevlieve it is similar but is only for limited time segments, which is what I need). What I did was:
function BeginButtonPushed(app, event)
for i = 1:length(app.strD)
app.timerCountdown.Value = app.strD(i);
pause(1);
end
end
% Button pushed function: TutorFunctionalityTest10sButton
function TutorFunctionalityTest10sButtonPushed(app, event)
timerVal = 10;
app.timerLeft = timerVal;
app.timerCountdown.Value = '00:00:10';
d = duration(0,0,10:-1:0);
app.strD = string(d);
end
end
Now this works for just seconds, but doesn't work for miniutes, as I cannot do duration(0,1:-1:0,10:-1:0). How would I be able to go about this? I am really confused, as if I just do duration (0, 1:-1:0) - it just prodcues a 1X2 duration array with 0:1:0 and 0:0:0. All help is appriciated.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 22 Apr 2022

Commented:

on 24 Apr 2022

Community Treasure Hunt

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

Start Hunting!