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

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).


Ken & Mike work on the MATLAB Desktop team.
  • DP: Hi i have a problem with ezplot3, i want to plot more than i curve in the same graph but hold on command...
  • Ken: Hi Arsalan, Unfortunately there is no way to get the new Editor API in older versions of MATLAB. -Ken
  • Arsalan: Hi, I am very excited about the MATLAB API for editor because right now i am working on a project and i need...
  • Johannes: Since I started using matlab-emacs some days ago I never experienced Emacslink. But I experienced some...
  • Francisco J. Beron-Vera: Hi all, I have recently learned about ViEmu (http://www.vimemu.c om) which, for Vi/Vim...
  • OysterEngineer: When I first learned of the Publish feature in MatLab, I thought it might be useful to help to...
  • Ken: Hi Herve, I’m not quite sure what you mean by “stand-alone&# 8221; mode? -Ken
  • Herve: I wonder when the publish fonction will be supported in standalone mode.
  • Mike: Ravi, What you described should work as far I understand it. Please follow up with technical support. With a...
  • Mike: @Daniel, Thanks for that note.

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