MATLAB Answers


how can I create a timer?

Asked by Nicolò
on 20 Apr 2013

hello everyone, can I insert a timer which expired at the time the user has to answer the question automatically take the input as wrong?



No products are associated with this question.

2 Answers

Answer by Mel
on 21 Apr 2013
 Accepted answer

Depends on how you are taking input from the user. Here are a couple ideas:

(1) tic toc

Use "tic" to start the timer just before you give the user the question. After the person has answered the question, you can add something like this:

if tElapsed>tmax
  display('You took too long to answer.')
  % Mark question as wrong

(2) uicontrol

I don't know the details of this (just looked through the help for a bit), but you can use uiwait(f,TL) to wait until figure f is closed of TL seconds has elapsed. So maybe you could do something like this?

h=uicontrol('String','Test'); % Put something in this line so that when the button is pressed, it closes figure f and sets ans to something non-zero.
if ans==0
  display('You took too long')
  % Mark question as wrong


Answer by per isakson
on 21 Apr 2013
Edited by per isakson
on 21 Apr 2013

There is a class named timer in Matlab. See the on-line help.

However, I guess you are looking for timeout. Search for timeout in the File Exchange. There are a few relevant entries.

And there is a relevant blog post: Using UIWAIT/UIRESUME to control program flow

The function, uiwait, has an input argument, timeout. The function, inputdlg, uses uiwait. However, inputdlg does not expose the timeout-option.

Maybe a wrapper around inputdlg will do the trick. I made a little experiment

    prompt = {'Enter matrix size:','Enter colormap name:'};
    dlg_title = 'Input for peaks function';
    num_lines = 1;
    def     = {'20','hsv'};
    timeout = 6;
    tmr = timer('TimerFcn',@(~,~)my_resume,'StartDelay',timeout,'TasksToExecute',1);
    start( tmr )
    answer = inputdlg( prompt, dlg_title, num_lines, def );
    disp( '-- the line after inputdlg --' )
    stop( tmr ), delete( tmr ) 


    function my_resume()
        h = findall( 0, 'Type', 'figure', 'Name', 'Input for peaks function' ); 
        uiresume( h )

This runs. However, the answer returned is empty, i.e. the [Cancel] alternative. Wrapping inputdgl might not be a possibility. How to distinguish between the user clicking cancel and the timeout?

It is doable. Possibilites:

  • modify a copy of inputdlg
  • doing something from scratch using uiwait with timeout.


The source code for inputdlg function is available. You can simply copy the source code to take an additional timeout argument and and modify the uiwait line to use this time.

Yes, I added "a copy of" I in my answer above.

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!