| MATLAB® | ![]() |
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:
Syntax errors — For example, misspelling a function name or omitting a parenthesis.
Run-time errors — These errors are usually algorithmic in nature. For example, you might modify the wrong variable or code a calculation incorrectly. Run-time errors are usually apparent when an M-file produces unexpected results. Run-time errors are difficult to track down because the function's local workspace is lost when the error forces a return to the MATLAB base workspace. The process of isolating and fixing these run-time problems is referred to as debugging.
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. | |
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. | |
Other Debugging Techniques |
| |
Cells | In the Editor, isolate sections of an M-file, called cells, so you can easily make changes to and run a single section. | |
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. | |
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. |
![]() | Running M-Files in the Editor | M-Lint Code Analyzer | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |