View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

4.4 | 14 ratings Rate this file 41 Downloads (last 30 days) File Size: 2.93 KB File ID: #12628 Version: 1.0
image thumbnail



Joseph Kirk (view profile)


13 Oct 2006 (Updated )

Displays elapsed time from keyboard or button inputs

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

This is a simple stopwatch GUI. It uses the functions CLOCK and ETIME along with user inputs to display the elapsed time in a HH:MM:SS.SSS format.

1) Press the START button to begin the timer (or press any key except L, R, or X)
  a. If the timer has already been activated, press the PAUSE button to stop the timer (or press any key except L, R, or X)
  b. If the timer has been paused, press the RESUME button to continue from the paused time (or press any key except L, R, or X)
  c. If the timer is in lap mode, press the RESUME button to continue as though the lap time had not been activated (or press any key except L, R, or X)
2) Press the LAP button to view lap times (or press the L key)
  a. The LAP button can be pressed successively to view mulitple laps
3) Press the RESET button to restore the timer (or press the R key)
4) Press the EXIT button to close the timer window (or press the X key)

Help notes available.

This file works great for timing myself speed solving the Rubik's Cube. :)

1. This file has been reworked to use nested functions and a timer rather than persistent variables. This allows the user to run multiple independent instances.
2. I also included the capability to start the stopwatch with a time offset. The offset can be positive or negative. If it is negative, the stopwatch will work like a countdown (see examples).
3. It is also possible to initialize the stopwatch with a 6 element vector in CLOCK format, which will cause the stopwatch to start running as soon as it is called (see examples).

% start the stopwatch

% start the stopwatch with a positive offset

% count down from one hour

% start the stopwatch with time already running

% start the stopwatch with running time since the day began
time = clock;
time(4:6) = 0;

% count down the time until midnight
time = clock;
time(3:6) = [time(3)+1 0 0 0];

MATLAB release MATLAB 7.6 (R2008a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (21)
09 Sep 2016 Anya Getman

LOVE it -- need to start with negative time to time Toastmasters speeches, but continue to be able to tell them how much they went over. Lap timer wonderful for sub-speech breakdowns.

29 May 2016 Aliza Hashmi

Where is the code of stopwatch? I couldn't find it

Comment only
31 May 2013 Aseel Al-Omari

thanks ... it has been very useful

09 Apr 2013 tony karamp

Very nice work!
But I have a question. I want to add this function into a GUI and I want, everytime I press a specific pushbutton, to store the exact timestamp (so basically the time I push the button) to a buffer. Any help as to how to do that? I know I have to add code to the callback function of the pushbutton, but I am not sure exactly what to write. Thank you in advance.

03 Apr 2013 the moon day

Wow good work

25 Feb 2009 Ruben Ensalzado

Very useful.. i'm working in an image acquisition gui, and i was about finish.. then i realized, that some people in the lab may need to know when the study is over or the elapsed time of the test.. Thanks to your gui, i can use some of your programming to finish my work on time!!

Thank you very much Joseph!

08 Sep 2008 rafau banan


22 Jun 2008 Arun Powar

I added some useful coding into program.. working nice..

19 Jun 2008 emre beyaz

i love it

24 Apr 2008 The Author

I reworked this clock to use nested functions and a timer rather than persistent variables. Now multiple instances of the stopwatch can be running simultaneously and independently, and users don't have to close the figure window to work on other things.

I also added some new functionality, including:
(1) the ability to "seed" the stopwatch with a (positive or negative) time offset. A negative offset will cause the stopwatch to function like a countdown (see examples), and
(2) the ability to initialize the stopwatch with a 6 element vector in CLOCK format to count down to (or up from) a fixed date/time. In this mode, the stopwatch will begin running as soon as it is called (see examples).

Comment only
31 Mar 2008 Tuss P.

Hello there. Great job, Joseph Kirk. Your program gives me an idea on how to measure a time spent in the program with beautiful GUI.

My intention is to write a file that can log presence of data as a time series like for example, x(t). I am trying to write a program to generate random circles and the program can record a time stamp for each location (x-y coordinates). I think it is possible but still struggling on how to do it. Please kindly give me some hints or advices.

Thank you again for your stopwatch,

10 Jan 2008 Vojta Nemec

How can I swith this function to script?Thanks

01 Jun 2007 The Author

Kevin - Excellent idea. I'll see what I can do

Comment only
26 May 2007 Kevin Gandalf

Very nice. Great useful stopwatch. It would be great if the author can expand this to have multiple stopwatches on the window.

22 Mar 2007 Dave Hensick

Very good. Nice display, easy to use, and no problems that I saw.

27 Dec 2006 abc def

08 Nov 2006 Joseph Kirk

Jos: I appreciate your comments. I made the changes as you suggested and added lap-time functionality. :)

Comment only
16 Oct 2006 Jos x@y.z

1) make it a function
2) provide a proper help section
3) format the output nicely using fprintf, so that when even I finally solves the rubic cube, I
don't get answers like elapsed-time = 372837482.3590

tic;pause;toc + enter solves the typing issue ...

16 Oct 2006 Joseph Kirk

Some people may not find this function useful, but it is perfectly suitable for some types of timing events.

I use it for repeated attempts to speed solve the Rubik's Cube. Solving a cube (usually) requires two hands, so having to type in 'toc' would add undesirable seconds to the time. It is much more efficient to tap the spacebar when the cube is solved. :)

Granted, MATLAB has much more productive uses than this, but this is the best system I have been able to come up with for timing events that require 2 hands. Perhaps there are others who could use it as well.

Comment only
16 Oct 2006 John D'Errico

Even with the addition of useful help, is this of any value?

To execute this function, you must type the function name (stopwatch) followed by a carriage return. Then any keypress to start timing, then another keypress to stop it. This takes a total of 12 keystrokes.

Simply typing tic (then carriage return) to start timing, and then toc (and another carriage return) to stop timing takes a total of 8 keystrokes.

I don't see where stopwatch gains anything.

Comment only
13 Oct 2006 Jos x@y.z

Wow, a real stopwatch for $1000 !
What about lap-times?

Sorry, but this is really too simple. Moreover, it is not a function, and does not contain useful help.

Comment only
08 Nov 2006

1. Modified to be a function instead of a script
2. Modified to allow a user specified number of laps
3. Added a help section with examples
4. Improved time display format

09 Jan 2007

Updated Information, Description

02 Mar 2007

Transformed the stopwatch into a GUI that can display elapsed time continuously or in *laps*

08 Mar 2007

Updated m-file

23 Apr 2008

Redesigned the code to use nested functions and a timer rather than persistent variables, added multiple instances capability, and included an optional time offset input.

Contact us