How can I put a timer in my GUI?

I want a code that is counting all the time since I press the push button. If the time is 3 s I want to display a message if the time is between 3s and 6s I want another message and between 6s and 20s the final message.
Could someone help me?

 Accepted Answer

t1 = tic ;
count =0 ;
while count ==0
if abs(3-toc(t1))<=10^-5
disp('time is 3S') ;
elseif toc(t1)>3 && toc(t1)<6
disp('time is between 3S and 6S')
elseif toc(t1)>6 && toc(t1)<20
disp('time between 6S and 20S')
end
end
toc(t1)

5 Comments

Hello! I have to write this in the final button? what I want to mean is that I have two buttons. With the first one my timer starts and with the second one depende on what time I push it, it would give me different message
You have to use tic and toc. On pressing one button start tic, on pressing other, get toc and depending on toc value you display the message.
Collegue
Collegue on 28 Mar 2017
Edited: KSSV on 28 Mar 2017
This would be correct? What I want is in the FirstButton when I push it only to start counting and when I push the Seconbutton to tell me the message depending on what time I have pushed it
function Firstbutton_Callback(hObject, eventdata, handles)
t1 = tic ;
count =0 ;
while count ==0
if abs(3-toc(t1))<=10^-5
disp('time is 3S') ;
elseif toc(t1)>3 && toc(t1)<6
disp('time is between 3S and 6S')
elseif toc(t1)>6 && toc(t1)<20
disp('time between 6S and 20S')
elseif toc(t1)>20
stop(t1)
end
end
function Secondbutton_Callback(hObject, eventdata, handles)
toc(t1)
You may need not to use this while loop....when you press button 1, call the tic, when you press the second button call the toc...it gives time..depending on it's value display the message.
This would be okey?
function firstbutton_Callback(hObject, eventdata, handles)
setappdata(H.Fig, 'Started', now);
function secondbutton_Callback(hObject, eventdata, handles)
elapsed = (now - getappdata(H.Fig, 'Started')) * 86400;
if elapsed <= 3.0
set(handles.text2, 'String', '<= 3');
elseif elapsed <= 6.0
set(handles.text2, 'String', '<= 6');
elseif elapsed <= 20.0 % Or simply "else"?
set(handles.text2, 'String', 'final message');
else
set(handles.text2, 'String', 'too slow');
end

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Tags

Asked:

on 28 Mar 2017

Commented:

on 28 Mar 2017

Community Treasure Hunt

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

Start Hunting!