File Exchange

image thumbnail


version (2.93 KB) by Joseph Kirk
Displays elapsed time from keyboard or button inputs


Updated 23 Apr 2008

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

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];

Cite As

Joseph Kirk (2021). Stopwatch (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (24)


Very helpful utility.
But multiple instances don't start until 'CTL+C' is pressed on the keyboard (which itself gives an error).
stopwatch(clock) --- when called four times for instance, will not display any clock until 'CTRL+C' is done manually at the keyboard.
Where to tweak in the code so that no keyboard intervention is required and no keypress is required and the multiple timers simply start running by calling 4 instances of stopwatch(clock) simultaneously ?

Park Habit

Thanks a lot!

Slf Sr

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.

Aliza Hashmi

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

Aseel Al-Omari

thanks ... it has been very useful

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.

the moon day

Wow good work

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!

rafau banan


Arun Powar

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

emre beyaz

i love it

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).

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,

Vojta Nemec

How can I swith this function to script?Thanks

The Author

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

Kevin Gandalf

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

Dave Hensick

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

abc def

Joseph Kirk

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

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 ...

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.

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.

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.

MATLAB Release Compatibility
Created with R2008a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!