How to build a timer in GUI?

9 views (last 30 days)
Meng Zhang
Meng Zhang on 5 Nov 2015
Edited: Meng Zhang on 8 Nov 2015
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.

Accepted Answer

Geoff Hayes
Geoff Hayes on 7 Nov 2015
Meng - the full error message includes the text
Index exceeds matrix dimensions.
which tells you that the code is trying to access an element in a matrix (or array) using an index that is outside the bounds of the matrix (or array). If you execute the line of code
dbstop if error
then call your above script, the MATLAB debugger will pause at the line that is throwing the error (line 40) and allow you to investigate the problem. In this case, the inputs to etime are both empty matrices. This is because of the two lines of code
time=etime(t2,t1);
set(DISPLAY,'string',time)
which are evaluated as soon as the GUI is created, immediately after the following line is called
DISPLAY = uicontrol('style', 'text','fontsize',20,'position',[250 100 300 150],'backgroundcolor',[0,0,1]);
Since your call to etime depends upon both t1 and t2 where each is initialized in the callback to the start and stop buttons respectively. Since neither button has been pressed, then neither variable is defined. Hence the error. The important thing to be aware of when creating a script like this, is that each line outside of a callback or other function will be evaluated once the script is run. In your case, you don't want to evaluate the above two lines until the user has pressed the stop button. That means that they should be put in the stop button callback as
function stopfcn(varargin)
format short g
t2=clock;
time=etime(t2,t1);
set(DISPLAY,'string',time)
end
Try running your script with the above change and see what happens. Note also that you don't need to declare all of your local variables as global variables. Given how you have defined your script, the nested functions (like your callbacks) have access to the parent function's (my_stopwatch) local variables.
  1 Comment
Meng Zhang
Meng Zhang on 8 Nov 2015
Edited: Meng Zhang on 8 Nov 2015
Thank you so much, Geoff. I made the change you mentioned and it worked! Like you said I don't need to declare all local variables as global ones, but I do think I need to declare t1. I tried not to declare any and matlab detected an undefined variable 't1', so maybe t1 need to be global. Thank you again!

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!