How to build a timer in GUI?
Afficher commentaires plus anciens
I am really a matlab beginner. I want to create a timer in GUI. There will be a start button and a stop button, which users can press to start or stop the timer. There will also be a text box which will display the elapsed time. I've spent several hours working on this and searching relevant information on online. Right now my code looks like below:
function my_stopwatch(varargin)
% my_stopwatch is a program that will ask user to start and stop a timer
% it will also display the elapsed time
global time START STOP DISPLAY t1 t2
f1 = figure(1); % this creates figure 1 and give the handle f1
set(f1,'menubar','none') ; % this removes menubar from the figure
set(f1,'position',[250 300 800 600]); % this sets the figure position and figure size
START = uicontrol ('style','pushbutton','string','START','fontsize',20,'position',[150 350 200 150],'backgroundcolor',[0,1,0],'callback',@startfcn);
% this creates the start pushbutton and defines its position,string, font size, and background color,the program will respond when user push this button
STOP = uicontrol ('style','pushbutton','string','STOP','fontsize',20,'position',[450 350 200 150],'backgroundcolor',[0,1,0],'callback',@stopfcn);
% this creates the start pushbutton and defines its position,string,font size, and background color,the program will respond when user push this button
DISPLAY = uicontrol('style', 'text','fontsize',20,'position',[250 100 300 150],'backgroundcolor',[0,0,1]);
% this creates a text box and defines its position,and font size, and background color,the string of the text will show the elapsed time
function startfcn(varargin)
format shortg
t1=clock;
end
function stopfcn(varargin)
format shortg
t2=clock;
end
time=etime(t2,t1);
set(DISPLAY,'string',time)
end
matlab gave me an error like this:
Error in etime (line 40)
t = 86400*(datenummx(t1(:,1:3)) - datenummx(t0(:,1:3))) + ...
I appreciate any feedback or suggestions! Thank you in advance.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Desktop dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!