Tag: interrupt
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.