MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Review the Failing Code


If the function in which the error occurred is implemented as a MATLAB program file, the
error message should include a line that looks something like this:


surf


Error using surf (line 50)
Not enough input arguments.


The text includes the name of the function that threw the error (surf, in this case) and
shows the failing line number within that function's program file. Click the line number;
MATLAB opens the file and positions the cursor at the location in the file where the error
originated. You may be able to determine the cause of the error by examining this line
and the code that precedes it.


Step Through the Code in the Debugger


You can use the MATLAB Debugger to step through the failing code. Click the underlined
error text to open the file in the MATLAB Editor at or near the point of the error. Next,
click the hyphen at the beginning of that line to set a breakpoint at that location. When
you rerun your program, MATLAB pauses execution at the breakpoint and enables you to
step through the program code. The command dbstop on error is also helpful in
finding the point of error.


See the documentation on “Debug a MATLAB Program” on page 22-2 for more
information.


Getting an Exception in Your Program Code


When you are writing your own program in a program file, you can catch exceptions and
attempt to handle or resolve them instead of allowing your program to terminate. When
you catch an exception, you interrupt the normal termination process and enter a block of
code that deals with the faulty situation. This block of code is called a catch block.


Some of the things you might want to do in the catch block are:



  • Examine information that has been captured about the error.

  • Gather further information to report to the user.

  • Try to accomplish the task at hand in some other way.


Exception Handling in a MATLAB Application
Free download pdf