Mike on the MATLAB Desktop

May 19th, 2008

Stop in the name of error

Recently I was performing some maintenance on the Dependency Report tool. The report has logic to display something reasonable in the web browser even if the scanned directory contains invalid MATLAB files. To make that work, there are several try/catch blocks that the process various errors that may be encountered.

When I tested my new changes in a directory with some “bad” MATLAB files…let’s just say that the code was not handling the errors properly! Normally in these situations I type:

dbstop if caught error

and inspect the state of program in the editor. However due to the recursive, iterative, and nested nature of this code, there were too many caught errors in different files to make this an efficient process. That’s when I remembered that we can stop on a particular type of error, but I could not remember the syntax to do so.

Debug menu to the rescue!

I clicked Debug -> Stop if Errors/Warnings… to open the Stop if Errors/Warnings for All Files dialog.

The debug menu

Next, I needed a unique error identifier, so MATLAB would know where to halt execution. This is easy to do from the command prompt using the lasterror command:

>> lasterror

ans = 

       message: 'Index exceeds matrix dimensions.'
    identifier: 'MATLAB:badsubscript'
         stack: [1x1 struct]

Then, I cut and pasted the identifier into the Stop if Errors/Warnings for All Files dialog. To follow along, Click the “Try/Catch Errors” tab (1), select “Use Message Identifiers” (2), and click “Add” (3):

The debug menu


Finally, I pasted in the message I was interested in and clicked “OK”:

The debug menu

After this, I re-ran my code. Execution stopped only when this error was caught, which allowed me to debug just this issue.

Incidentally, the command-line syntax is shown in the dialog; hopefully I will remember that for next time!

3 Responses to “Stop in the name of error”

  1. Mike replied on :

    I should mention, dear readers that you of course should not use the lasterror function. This function depends on global state, and its programmatic use is not encouraged.

    At the command line, MException.last contains all of the information in lasterror.

  2. Christian Hachmann replied on :

    Hi,
    when I enter debug mode via a breakpoint inside a for-loop and enter the command “break” at the prompt it does not behave as it would in the code. I get

     ??? Error: A BREAK statement appeared outside of a loop. Use RETURN instead.

    The continue-statement does not seem to work in debug-mode either.
    So i would appreciate a way to skip execution of the rest of the loop and continue execution of the code after the loop.

  3. Mike replied on :

    @Christian,

    This is not possible. When you run commands from the debug prompt, they are evaluated in a separate program-flow context from the running program (although it does have the appropriate workspace).

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks
Mike works on the MATLAB Desktop team.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.