Finding Errors, Debugging, and Correcting M-Files

This section introduces general techniques for finding errors and using the M-Lint automatic code analyzer to detect possible areas for improvement in M-files. It then illustrates the MATLAB® debugger features in the Editor, as well as equivalent Command Window debugging functions, using a simple example.

There are two kinds of errors:

In addition to finding and fixing problems with your M-files, you might want to improve the performance and make other enhancements using MATLAB tools.

Use the following techniques to isolate the causes of errors and improve your M-files.

Technique or Tool

Description

For More Information

Syntax highlighting and Delimiter matching

Syntax highlighting helps you identify unterminated strings in an M-file before you run the file.

Delimiter matching helps you correctly match pairs of parentheses, brackets, braces, and keywords.

Syntax Highlighting

Matching Delimiters (Parentheses)

Error Messages

When you run an M-file with a syntax error, MATLAB software will most likely detect it and display an error message in the Command Window describing the error and showing its line number in the M-file. Click the underlined portion of the error message, or position the cursor within the message and press Ctrl+Enter. The offending M-file opens in the Editor, scrolled to the line containing the error.

To check for syntax errors in an M-file without running the M-file, use the pcode function.

None

M-Lint

Use the M-Lint code analyzer to help you verify the integrity of your code and learn about potential improvements. Access M-Lint messages automatically while you work in a file in the Editor, or run an M-Lint report for an existing file.

To evaluate the McCabe complexity (also known as the cyclomatic complexity) of an M-File, use the mlint function with the -cyc option.

M-Lint Code Analyzer and the reference page for the mlint function

Editor, Graphical Debugger, and MATLAB Debugging Functions

The MATLAB Editor, graphical debugger, and MATLAB debugging functions are useful for correcting run-time problems because you can access function workspaces and examine or change the values they contain. You can set and clear breakpoints, indicators that temporarily halt execution in an M-file. While stopped at a breakpoint, you can change workspace contexts, view the function call stack, and execute the lines in an M-file one by one.

Debugging Process and Features

Other Debugging Techniques

  • Add keyboard statements to the M-file—keyboard statements stop M-file execution at the point where they appear and allow you to examine and change the function's local workspace. This mode is indicated by a special K>>prompt. Resume function execution by typing return and pressing the Enter key. For more information, see the keyboard reference page.

  • Remove selected semicolons from the statements in your M-file—semicolons disable the display of output in the M-file. By removing the semicolons, you instruct MATLAB to display these results on your screen as the M-file executes.

  • List dependent functions—use the depfun function to see the dependent functions.

Reference pages for keyboard and depfun function

Cells

In the Editor, isolate sections of an M-file, called cells, so you can easily make changes to and run a single section.

Using Cells for Rapid Code Iteration and Publishing Results

Profiler

Use the Profiler to help you improve performance and detect problems in your M-files. Access the Profiler from the Editor by selecting Tools > Open Profiler.

Profiling for Improving Performance

Directory Reports

The M-file Directory Reports help you polish and package M-files before providing them to others to use. Access all of these tools from the Current Directory browser. You can access some of these directly from the Editor Tools menu.

Directory Reports in Current Directory Browser

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS