| MATLAB® | ![]() |
| On this page… |
|---|
What Is the M-Lint code Analyzer? M-Lint Automatic Code Analyzer in the Editor |
The M-Lint code analyzer checks your code for problems and recommends modifications to maximize performance and maintainability.
You can use M-Lint in two different ways, both of which report the same information:
Run a report for an existing M-file or group of M-files. To do so, from an M-file in the Editor, select Tools > M-Lint > Show M-Lint Report. Make any changes to your file based on the M-Lint messages in the report. After making changes, you must save the file and rerun the report to see if your changes addressed the issues noted in M-Lint messages. To run M-Lint for all files in a directory, access M-Lint from the Current Directory browser—select View > Directory Reports > M-Lint Code Check Report. For details, see M-Lint Code Check Report.
Continuously check code in the Editor while you work. View M-Lint messages and make changes to your file based on the messages. The code analyzer updates automatically and continuously so you can see if your changes addressed the issues noted in the M-Lint messages. For some messages, M-Lint offers automatic code correction. For details about specific M-Lint messages, see M-Lint Code Check Report. Information about using the continuous checking and correction interface in the Editor is explained here.
To use the M-Lint continuous code checking in an M-file in the Editor, perform the following steps:
Ensure the M-Lint messaging preference is enabled: Select File > Preferences > M-Lint and select the Enable integrated M-Lint warning and error messages check box. To follow these instructions, be sure the Underlining option is set to Underline warnings and errors.

Click OK.
Open an M-file in the Editor. This example uses the sample file lengthofline.m:
Open the example file:
open(fullfile(matlabroot,'help','techdoc','matlab_env',...
'examples','lengthofline.m'))
Save the example file to a directory to which you have write access. For the example, lengthofline.m is saved to I:\MATLABFiles\mymfiles.
The M-Lint message indicator at the top right edge of the window conveys the M-Lint messages reported for the file:
Red means syntax errors were detected. Another way to detect some of these errors is using syntax highlighting to identify unterminated strings, and delimiter matching to identify unmatched keywords, parentheses, braces, and brackets.
Orange means warnings or opportunities for improvement were detected, but no errors were detected.
Green means no errors, warnings, or opportunities for improvement were detected.
For the example, the indicator is red, meaning there is at least one error in the file.

Click the M-Lint message indicator to go to the next code fragment containing an M-Lint message. The next code fragment is relative to the current cursor position, viewable in the status bar.
In the lengthofline example, the first message is at line 22. The cursor moves to the beginning of line 22.
The code fragment for which there is an M-Lint message is underlined in either red for errors or orange for warnings and improvement opportunities.
To view the M-Lint message, move the pointer within the underlined fragment. The message appears with a yellow highlighted background, similar to data tips (see Viewing Values as Data tips in the Editor).

This message means that in line 22, nothandle is assigned a value, but is probably not used anywhere after that in the file. The line might be extraneous and you could delete it. But it might be that you actually intended to use the variable, as shown in step 6 of this example.
Make changes to your code as needed. The M-Lint indicator and underlining automatically update to reflect the changes you make, even if you do not save the file.
In this example, the intention was to use nothandle as a performance improvement by determining the value prior to the loop. Changing ~ishandle(hline(nh)) in line 24 to nothandle(nh) means there is no longer an M-Lint message associated with line 22. For more information about what the warning and improvement messages in this example mean and actions you can take to address them, see Messages and Resulting Changes for the lengthofline Example.
Some errors and warnings are highlighted, indicating M-Lint can automatically fix the code. For example, in lengthofline, line 23, prod is underlined because there is an M-Lint warning, and it is highlighted because an automatic fix is available. When you view the M-Lint message, it also indicates the auto-fix that is available.


Right-click the highlighted code (for a single-button mouse, use Ctrl+click). The first item in the context menu indicates the automatic fix that M-Lint can perform. Select it and M-Lint automatically corrects the code. In this example, M-Lint replaces prod(size(hline)) with numel(hline).

There is a preference you can set for the color—for more information, see Other Colors.
You might want to ignore certain M-Lint messages and do not want the messages to display; for more information, see Suppressing M-Lint Indicators and Messages.
You can click the M-Lint message indicator to go to the next message, or use the other way to view messages, which is the M-Lint message bar. Each marker in the bar represents a line that has associated M-Lint messages. A red marker means there is an error at that line, while an orange marker means there are warnings or suggested improvements, but no errors at that line.
Position the pointer at a marker in the message bar to view the message. For example, to see an error in lengthofline, position the pointer at a red marker in the message bar. There is only one error in the file and with the pointer positioned over it, the associated M-Lint messages appears. Click the marker to go to the first code fragment in the line that resulted in an M-Lint message. For the example, click the red marker, which takes you to the first suspect code fragment in line 48.
temp = diff([data{1}(:) data{2}(:) data{3}(;)]);Multiple messages can represent a single problem or multiple problems. Addressing one might address all of them, or after addressing one, the other messages might change or what you need to do might become clearer.

Make changes to address the problem noted in the M-Lint message—the M-Lint indicators update automatically.
In the example, the M-Lint message suggest a delimiter imbalance. You can check that by moving the arrow key over each of the delimiters to see if MATLAB® software indicates a mismatch. This requires that File > Preferences > Keyboard > Delimiter Matching has the Match on arrow key option selected. There are no mismatched delimiters. The actual problem is the semicolon in parentheses, data{3}(;), is incorrect and should be a colon. In line 48, change data{3}(;) to data{3}(:). When you make the change, the underline no longer appears in line 48. That single change addressed the issues in all of the M-Lint messages for line 48.
Because the change you made removed the only error in the file, the M-Lint message indicator at the top of the bar changes from red to orange, indicating that only warnings and potential improvements remain.
If there are multiple messages associated with a line, there might be multiple underlined code fragments that are adjacent, as in the above example, making it difficult to display the message of interest. In those cases, it might be easier to view the messages via the marker on the message bar.
After making changes to address all M-Lint messages, or disabling designated messages, the M-Lint message indicator becomes green The example file with all M-Lint messages addressed has been saved as lengthofline2.m, which you can open by running
open(fullfile(matlabroot,'help','techdoc',... 'matlab_env', 'examples','lengthofline2.m'))

Depending on what stage you are at in completing the M-file, you might want to restrict the underlining, which you can do via the M-Lint preference referred to in step 1, above. For example, when first coding, you might prefer no underlines because they would be distracting. For details, click the Help button in the Preferences dialog box.
M-Lint does not provide perfect information about every situation and in some cases, you might not want to make any changes based on an M-Lint message. In the event you do not want to change the code but you also do not want to see the M-Lint indicator and message for that line, instruct M-Lint to suppress them. For the lengthofline example, in line 49, the first M-Lint message is Terminate statement with semicolon to suppress output (in functions). Adding a semicolon to the end of a statement suppresses output and is a common practice. M-Lint alerts you to lines that produce output but lack the terminating semicolon. If you want to view output from line 49, do not add the semicolon as M-Lint suggests.
There are a few different ways to suppress the M-Lint indicators and messages:
Note that you cannot suppress M-Lint error messages such as syntax errors, and therefore, the following options do not apply.
Right-click at the M-Lint underline (for a single-button mouse, use Ctrl+click). From the context menu, select Ignore this "Terminate statement with semicolon...". M-Lint adds a %#ok<NOPRT> to the end of the line, which instructs MATLAB not to check for a terminating semicolon at that line. M-Lint removes the underline and mark in the M-Lint indicator bar for that message.
If there are two messages on a line that you do not want M-Lint to display, right-click separately at each underline and select the appropriate entry from the context menu. M-Lint expands the %#ok syntax. For the example, ignoring both messages for line 49 would add %#ok<NBRAK,NOPRT>.
For more information about %#ok, see the mlint function reference page.
This method of suppressing the messages changes the M-file. If M-Lint preferences are to set to enable this message, the specific instance of the message suppressed in this way will not appear because the %#ok takes precedence over the preference setting. If you later decide you want M-Lint to check for a terminating semicolon at that line, delete the %#ok<NOPRT> from the line.


Right-click at the M-Lint underline (for a single-button mouse, use Ctrl+click). From the context menu, select Disable all "Terminate statement with semicolon...". Doing so modifies the M-Lint preference setting, which applies to all occurrences in all files, unless a line includes a %#ok for that message. For more information about the M-Lint preference, including how to restore MATLAB default settings, select File > Preferences > M-Lint, and click Help.
Use M-Lint preferences by selecting File > Preferences > M-Lint. Then enable specific messages or categories of messages and save the settings to a txt file. You can reuse the settings for any M-file, or provide the settings file to another user.
To use the saved settings, either select the settings file in M-Lint preferences, or in the Editor. In the Editor, right-click the M-Lint message bar (for a single-button mouse, use Ctrl+click), or select Tools > M-Lint. The currently-selected setting choice is shown, preceded by a bullet point. You can choose from any of the settings files, such as the MLintNoSemis example, as shown here.

For more information about M-Lint settings and preferences, click Help in the M-Lint preferences panel.
Under some circumstances, when you are editing an M-file, M-Lint can cause the MATLAB session to terminate unexpectedly. The next time you start MATLAB, it displays the following message and disables M-Lint for the M-file that was open in the editor when MATLAB terminated.
M-Lint caused your previous MATLAB session to terminate unexpectedly. Please send this message and file name to The MathWorks. See "About M-Lint and Unexpected MATLAB Termination" in the MATLAB documentation for details.
If you want, while waiting for a response from The MathWorks™, you can attempt to reenable M-Lint for the file by following these steps:
In the Editor, reopen the file that you were editing when MATLAB terminated.
Remove the lines of code that you believe M-Lint could not handle.
In a text editor, open the MLintFailureFiles file in your preferences directory. (This is the directory that MATLAB returns when you run prefdir.)
Save and reopen the M-file.
![]() | Finding Errors, Debugging, and Correcting M-Files | Debugging Process and Features | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |