Random Wisdom

MATLAB – Interrupting function execution

by on Mar.02, 2007, under How To ..., Software

MATLAB itself does not provide any way of arbitrarily interrupting/pausing a function at mid-execution to examine the internal function stack/variables. It is a pity since such a mechanism would be an extremely valuable debugging tool.

Finally, I realized that this is not as impossible as it seems. In fact, it is EXTREMELY simple! All that is required are four lines of code in any script/function:

[statcode, result] = system('ls sometoken');
if statcode == 0
  keyboard
end

The system command is used to run system console commands. Here, a simple check is made for the existence of the file ‘sometoken’ in the current directory. If the file exists, statcode is set to zero and the if condition is satisfied. The keyboard command is then executed which causes MATLAB to pause execution and return control to the command line! In order to continue execution, ‘sometoken’ must be deleted/removed from the current directory and the command return must be issued.

The best place for this code is perhaps in any loop that might exist in the MATLAB script/function. Then, to cause execution to pause, one simply needs to create a file ‘sometoken’ in the current directory.

:,

13 Comments for this entry

  • Daniel

    Thanks for this excellent idea.

    But I had to use some other command becasue 'ls' wasn't working on my computer.

    My alternative is:

    pausecmd = dir('sometoken');
    if size(pausecmd,1) > 1
    delete('sometoken')
    keyboard
    end

    As you can see I added an extra line to automatically delete the created file.

  • DarkKnight

    Thanks for the comment/enhancement!

  • G R

    This is a handy little trick. As an alternative, one could use exist.

    statcode=exist(‘sometoken’);
    if statcode==2,
    delete(‘sometoken’);
    keyboard
    end

    Also, I usually have a second instance of matlab operational and can use the following to automatically generate the file.

    function []=token()
    fclose(fopen(‘sometoken’,’w’));

  • Mostafa

    Hello G R,

    thanks for the suggestion. It’s always nice to hear from people who actually find that trick useful 🙂

  • Uwe

    Awesome, that’ll work for me.

  • Paul (India)

    Thanks for the excellent idea. Very useful functionality for debugging. Wonder why matlab guys didn’t provide this in the first place…

  • matlab

    use breakpoint

  • Mostafa

    @”matlab”

    I think you are missing the point. The method described in the article will let your program run as normal until you decide that you want it to break. This is not the case with breakpoints which must be configured before the function is executed.

  • Luis

    Thank you for you code! .. i added some extra lines for making a button. Place this at beginning of your function:

    h = figure(‘ToolBar’,’none’,);
    ht = uitoolbar(h);
    hpt = uipushtool(ht,…
    ‘TooltipString’,’Toolbar push button’,…
    ‘ClickedCallback’,…
    ‘keyboard’);

    Saludos!

  • shabnam

    i need when my program is running and i press any key it do sth without any asking me

  • Isaiah

    Wow, this is perfect.
    I was just thinking of how simple this task is in assembly language, but MATLAB has to make it difficult!

    I am generating thousands of plots and playing them through a while loop as a movie. Long story short, this is the only way I have found that I can pause my movie at a point of interest, and resume playing afterwards.

    THANK YOU

  • Michelle

    Terrific solution to a common problem. Thanks!

1 Trackback or Pingback for this entry

Leave a Reply