MATLAB – Interrupting function execution
by Mostafa 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
1 Trackback or Pingback for this entry
-
How to interrupt a loop in MATLAB | Dustin E. Kruse, Ph.D.
March 29th, 2011 on 2:54 am[…] After some Google searching, I came across this clever method here. […]
September 10th, 2008 on 3:39 pm
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.
September 10th, 2008 on 4:29 pm
Thanks for the comment/enhancement!
March 8th, 2010 on 7:31 pm
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’));
March 8th, 2010 on 7:35 pm
Hello G R,
thanks for the suggestion. It’s always nice to hear from people who actually find that trick useful 🙂
January 26th, 2011 on 3:03 pm
Awesome, that’ll work for me.
September 20th, 2011 on 11:00 pm
use
pause;
November 25th, 2011 on 6:40 am
Thanks for the excellent idea. Very useful functionality for debugging. Wonder why matlab guys didn’t provide this in the first place…
March 2nd, 2012 on 2:50 pm
use breakpoint
March 2nd, 2012 on 5:39 pm
@”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.
July 17th, 2012 on 7:48 pm
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!
August 5th, 2012 on 9:22 am
i need when my program is running and i press any key it do sth without any asking me
March 4th, 2013 on 6:18 am
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
April 3rd, 2014 on 6:44 am
Terrific solution to a common problem. Thanks!