Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Ken & 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!

One Response 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.

Leave a Reply


Ken & Mike work on the MATLAB Desktop team.
  • Ken: Hi Rich, We have a bug report on the first issue you mention. As for the Command Window time-stamp format,...
  • Rich Messeder: I have a few comments that I’m trying to find the right place for. One is that when I press the...
  • Steve Eddins: Stephen—You can extend your R2008b Prerelease license by choosing the Help -> Licensing ->...
  • Mike: Stephen, We cannot give an exact date as to when the next version of MATLAB will be released.
  • Stephen Colona: Do you think the new release will come out before the PreRelease expires? Thanks.
  • Mike: Nasir, Thank you for your feedback, it helps us refine the desktop and ultimately make it easier to use....
  • Nasir: sent my desktop.
  • Mike: Han, We are always investigating new ways to enhance the MATLAB language and improve the desktop experience. I...
  • Han Geerligs: Hello Mike, first, thanks for mentioning me ;-) I was wondering if you plan to introduce more pragma...
  • Tom Clark: FOUR buttons? You’re spoiling yourselves :) Think about this… I’m pretty sure...

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

Related Topics