MATLAB Answers

per isakson

Spurious fire from timer

Asked by per isakson
on 6 Mar 2013

Question: Has anyone experienced the behavior, which I describe here?

Posting questions here makes me think. This is a better version of Weird behavior when debugging code invoked by timer.

I argue that it happens that a callback is spuriously fired during debugging of code invoked by a timer. Now, I think I have a way to demonstrate the behavior; that is to reproduce the behavior.


  1. R2012a 64bit, Win7
  2. put a break-point in the subfunction, index_out_of_bounds, at line 7
  3. start the function, spurious_fire_from_timer
  4. at the break-point execute stk1 = dbstack;
  5. open an m-file in the editor
  6. at the break-point execute stk2 = dbstack;
  7. inspect the stack-drop-down-list
  8. run whos and {}'
  9. click [Continue] (/F5)
  10. run whos and {}'
K>> whos
  Name       Size            Bytes  Class     Attributes
  a          1x1                 8  double              
  stk2      10x1              4164  struct 

Note that stk1 is not in this workspace and display the function names

K>> {}'
ans = 

[Continue] shows

    Error while evaluating TimerFcn for timer 'my_timer' 
    Index exceeds matrix dimensions.

and takes us back to the first break. Now, stk1 is in the current workspace

K>> whos
  Name      Size            Bytes  Class     Attributes
  a         1x1                 8  double              
  stk1      6x1              2582  struct              
K>> {}'
ans = 


  • This shows that opening an m-file caused Matlab to fire a callback.
  • I think this can be triggered by other user actions as well. However, I'm not sure which or whether it is intermittent.
  • While experimenting with this behavior yesterday Matlab once crashed "without smoke"; just dropped dead without any crash report.
  • This behavior does not occur (i.e. I failed to make it happen) with 'ExecutionMode' equal to 'singleShot'


    function    spurious_fire_from_timer()
        tmr = timer('Name'      , 'my_timer'        ...
            ,   'TimerFcn'      , @invoked_by_timer ...
            ,   'BusyMode'      , 'drop'            ...
            ,   'ExecutionMode' , 'fixedRate'       ...
            ,   'Period'        ,  1                ...
            ,   'StartDelay'    ,  1                ...     
            ,   'TasksToExecute',  1                ...
        start( tmr )
        wait ( tmr )


    function    invoked_by_timer( tmr, evnt )
        variable_not_used( tmr, evnt ) 
    function    index_out_of_bounds()
        a = 17;    
        b = 18;
        c = a(2);
        variable_not_used( a, b, c ) 


    function    variable_not_used( varargin )       
    % variable_not_used - helps keep the code analyzer box green  



No products are associated with this question.

0 Answers

Discover MakerZone

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

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!