| Products & Services | Industries | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
What Is the M-Lint Code Analyzer? Ways to Use the M-Lint Code Analyzer Using M-Lint Automatic Code Analyzer in the Editor |
The M-Lint code analyzer, also referred to as just M-Lint, checks your code for problems and recommends modifications to maximize performance and maintainability.
You can use M-Lint in three different ways, all of which report the same messages:
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 extended messages, automatic code correction, or both. Details about using the continuous checking and correction interface in the Editor is explained in Using M-Lint Automatic Code Analyzer in the Editor
Run a report for an existing M-file:
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.
Save the file.
Rerun the report to see if your changes addressed the issues noted in the M-Lint messages.
Run a report for all files in a folder:
In the Current
Folder browser, click the Actions button
.
Select Reports > M-Lint Code Check Report.
Make any changes to your files based on the M-Lint messages in the report.
For details, see M-Lint Code Check Report.
Save the file.
Rerun the report to see if your changes addressed the issues noted in the M-Lint messages.
To use the M-Lint continuous code checking in an M-file in the Editor, follow these steps:
Ensure the M-Lint messaging preference is enabled: Select File > Preferences > M-Lint, and then 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 that ships with the MATLAB software:
Open the example file:
open(fullfile(matlabroot,'help','techdoc','matlab_env',...
'examples','lengthofline.m'))
Save the example file to a folder 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 indicates 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 indicates warnings or opportunities for improvement, but no errors, were detected.
Green indicates 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.
View the M-Lint message by moving the mouse 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 7 of this example. Notice that the message is a link to additional information. M-Lint message links are discussed in steps 9 and 10.
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.
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 provides a button to apply the automatic fix. It also indicates the keyboard controls (Alt+Enter) to apply it.

To view what the automated fix is, 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).

After you apply the fix, M-Lint removes the underline from prod in line 23.

Click the M-Lint message indicator to go to the next message, on line 24. When you view this M-Lint message, it appears as a link to indicate that there is more information about the message. Not all messages have additional information.

Click the link in the M-Lint Message. The window expands to display an explanation and user action.

Click the M-Lint message indicator to go to the next message, or use the M-Lint message bar.
Each marker in the bar represents one or more lines that have associated M-Lint messages.
Position the mouse 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 (error) 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. (There can be multiple messages per line.) 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 following these steps:
Choose File > Preferences > Keyboard > Delimiter Matching, and then select Match on arrow key, if it is not already selected.
Move the arrow key over each of the delimiters to see if MATLAB indicates a mismatch.
In the example, it may appear that there are no mismatched delimiters. However, M-Lint detects the semicolon in parentheses: data{3}(;), and interprets it as the end of a statement. M-Lint reports that the two statements on line 48 each have a delimiter imbalance.
In line 48, change data{3}(;) to data{3}(:).
When you make the change, the underline no longer appears in line 48. M-Lint now interprets the code at line 48 as a single statement. The single change addresses the issues in both 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 previous step, making it difficult to display the message of interest. In these cases, it is easier to view the messages through the marker on the message bar than moving the arrow over each delimiter.
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. Open the example file with the command:
open(fullfile(matlabroot,'help','techdoc',...
'matlab_env', 'examples','lengthofline2.m'))

Depending on the stage at which you are in completing the M-file, you might want to restrict the M-Lint underlining. You can do this by using the M-Lint preference referred to in step 1, in Checking M-File Code for Problems Using the M-Lint Code Analyzer. For example, when first coding, you might prefer to underline only errors because warnings 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, and 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 (turn off) the M-Lint indicators for warning and error messages:
Suppress an Instance of an M-Lint Message in the Current File
Suppress All Instances of an M-Lint Message in the Current File
Understanding M-File Code Containing Suppressed M-Lint Messages
You cannot suppress M-Lint error messages such as syntax errors. Therefore, instructions on suppressing M-Lint messages do not apply to those types of messages.
You can instruct M-Lint to suppress a specific instance of a message in the current file. For example, using the code presented in Checking M-File Code for Problems Using the M-Lint Code Analyzer, follow these steps:
In line 49, right-click at the first M-Lint underline (for a single-button mouse, use Ctrl+click) .
From the context menu, select Suppress this Instance of "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, in the code presented in Checking M-File Code for Problems Using the M-Lint Code Analyzer, ignoring both messages for line 49 adds %#ok<NBRAK,NOPRT>.
Even if M-Lint preferences are set to enable this message, the specific instance of the message suppressed in this way does 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> string from the line.
For more information about %#ok, see the mlint function reference page.


You can instruct M-Lint to suppress all instances of a specific message in the current file. For example, using the code presented in Checking M-File Code for Problems Using the M-Lint Code Analyzer, follow these steps:
In line 49, right-click at the first M-Lint underline (for a single-button mouse, use Ctrl+click).
From the context menu, select Suppress all Instances in this File.
M-Lint adds a %#ok<*NOPRT> to the end of the line, which instructs MATLAB to not check for a terminating semicolon throughout the file. M-Lint removes all underlines from the code, as well as marks in the M-Lint indicator bar that correspond to this message.
If there are two messages on a line that you do not want M-Lint to display any instances of in the current file, right-click separately at each underline, and then select the appropriate entry from the context menu. M-Lint expands the %#ok syntax. For the example, in the code presented in Checking M-File Code for Problems Using the M-Lint Code Analyzer, ignoring both messages for line 49 adds %#ok<*NBRAK,*NOPRT>.
Even if M-Lint preferences are set to enable this message, the message does 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 in the file, delete the %#ok<*NOPRT> string from the line.
For more information about %#ok, see the mlint function reference page.
You can instruct M-Lint to disable all instances of an M-Lint message in all files. For example, using the code presented in Checking M-File Code for Problems Using the M-Lint Code Analyzer, follow these steps:
In line 49, right-click at the first M-Lint underline (for a single-button mouse, use Ctrl+click).
From the context menu, select Disable this Message in M-Lint Preferences.
This modifies the M-Lint preference setting. For more information about M-Lint preferences, including how to restore MATLAB default settings, select File > Preferences > M-Lint, and then click Help.
You can specify that you want certain messages to be disabled by default when you open any M-file. Typically, you do this if you find that you do not want certain messages or categories of messages to be enabled for all or most of your M-files.
Follow these steps:
Select File > Preferences > M-Lint
The Preferences dialog box opens and displays the M-Lint Preferences panel.
Disable specific messages, or categories of messages.
The Active Settings field now contains the value Default Settings (modified).
Click Apply.
Now, each M-file you open uses the modified default settings. If you want to restore the factory-installed default settings, decide if you want to save the current settings to a file, as described in Specify Nondefault M-File Message Settings, and then click Restore Defaults.
For more information about M-Lint settings and preferences, click Help in the M-Lint preferences panel.
You can specify that you want certain messages to be enabled or disabled, and then save those settings to a file. When you want to use a settings file with a particular M-file, you select it from the M-Lint preferences panel. That setting file remains in effect until you select another settings file. Typically, you change the settings file when you have a subset of M-files for which you want to use a particular settings file.
Follow these steps:
Select File > Preferences > M-Lint
The Preferences dialog box opens and displays the M-Lint Preferences panel.
Enable or disable specific messages, or categories of messages.
Click Save as and save the settings to a txt file.
Click OK.
You can reuse these settings for any M-file, or provide the settings file to another user.
To use the saved settings:
In the Editor, select Tools > M-Lint.
The currently-selected setting choice displays, preceded by a bullet point.
Choose from any of the settings files, such as the MLintNoSemis example, as shown here.
The settings you choose are in effect for all M-files until you select another set of M-Lint settings.

If you are given code that contains suppressed M-Lint messages, you might want to review those messages without the need to unsuppress them first. A messages might be in a suppressed state for any of the following reasons:
One or more %#ok<message-ID> pragmas are on a line of code that elicits an M-Lint message specified by <message-ID>.
One or more %#ok<*message-ID> pragmas are in a file that elicits the M-Lint message specified by <message-ID>.
It is deselected in the M-Lint Preferences panel.
It is disabled by default.
To determine the reasons why some M-Lint messages might be suppressed, follow these steps:
Search the M-file for the %#ok pragma and create a list of all the message-IDs associated with that pragma.
Open the M-Lint Preferences dialog box by choosing File > Preferences > M-Lint.
In the filter field, enter one of the message IDs, if any, you found in step 1.
The message list now contains only the message that corresponds to that ID. If the message is a hyperlink, you can click it to see an explanation and suggested action for the message. This might provide insight into why the message is suppressed or disabled. The following image shows how the Preferences dialog box appears when you enter CPROP in the filter field, for example.

Click the
button to clear the filter
field, and then repeat step 3 for each message ID you found in step
1.
Click the
button to clear the filter
field.
Filter the messages to display those that are disabled by default and disabled via the Preferences panel by clicking the down arrow to the right of the filter field, and then clicking Show Disabled Messages.
The message list now contains all the messages that are disabled in the Preferences panel.
Review the message associated with each message ID to understand why it might be suppressed in the code or disabled in Preferences.
Use M-Lint preferences to adjust how the M-Lint code analyzer operates. The preferences apply to M-Lint usage with the Editor, the Embedded MATLAB™ Editor (if you have products which use that tool), and the M-Lint Code Check Report, with a few exceptions that are noted. For more information about using M-Lint, see Checking M-File Code for Problems Using the M-Lint Code Analyzer.
This section contains information about the following topics:
Enabling integrated M-Lint warning and error messages in the Editor
Changing the Color M-Lint Uses to Indicate an Automatic Fix Is Available
Select this check box if you want the Editor to show M-Lint messages in the M-file. This preference does not apply to the M-Lint Code Check Report. When you select this preference, the Editor provides visual cues that alert you to potential errors and problems, as well as opportunities for improvement in your code. These visual cues take the form of underlines and a message indicator bar. From these cues, you can view a message for each line of an M-file that M-Lint indicates might be improved. For example, a common M-Lint message is that a variable is defined but never used in the M-file.
Underlining. Restrict the M-Lint underlining notification using the associated drop-down list. The list is available only when Enable integrated M-Lint warning and error messages is selected. Options are
Underline warnings and errors
Underline errors only
No underlines
Errors are underlined in red and warnings are underlined in orange. For all of the underlining options, the Editor provides the marks for errors and warnings in the indicator bar.
You might choose a different option at different stages in your workflow. For example, when first coding, you might prefer no underlines because they would appear as you enter a statement and might be distracting. Later, you might choose to underline only errors to help you debug your file. Finally, when tweaking an existing M-file, you might want to underline warnings and errors because the file is in a state that you can fix any issues you introduce at the time.
Autofix. Click Adjust autofix highlight color to open the Colors Preferences dialog box so that you can adjust the color that M-Lint uses to highlight errors and warnings that it can fix automatically. By default, this color is pale orange.
In the Color Preferences dialog box, in the Other colors group, use the M-Lint autofix highlight palette to select a color. For more information, see Checking M-File Code for Problems Using the M-Lint Code Analyzer.
When you have M-Lint warnings and error messages enabled, use M-Lint settings to show or hide specific messages for your code. If you are new to using M-Lint or MATLAB, use the default settings. After you are familiar with M-Lint and MATLAB, consider suppressing the display of certain M-Lint messages.
To suppress an M-Lint message on a line-by-line or file-by-file basis, see Suppressing M-Lint Indicators and Messages, or the mlint function.
To suppress messages in more than one file, it is more convenient to disable the M-Lint preference settings, as described here:
Use the filter field to filter the messages displayed in the Active settings table. For details, see Filtering M-Lint Messages.
Click the link for a given message (if available), to get more information about that message, including an explanation and suggested action.
In the Active settings table, select check boxes or clear check boxes to enable or disable messages, respectively. To enable or disable a set of messages simultaneously, highlight the messages in the Active settings table, right-click, and then choose Enable or Disable.

M-Lint shows enabled messages and hides disabled messages in the Editor.
Click Apply or OK to save the changes.
As with all preferences, MATLAB retains the settings for your next session.
Note The MATLAB Compiler (deployment) messages category is the only one that you can enable or disable by category. The M-Lint preferences panel only displays the MATLAB Compiler (deployment) messages category if you have MATLAB® Compiler™ installed. |
Filtering M-Lint Messages. You can filter the list of messages in the Preferences dialog box to display only those messages that are currently of interest to you. Use any combination of the methods that the following table presents.
Note If you do not have the MATLAB Compiler installed, the M-Lint preferences panel does not display the MATLAB Compiler (deployment) messages category. |
| To see a list of messages that: | Perform this action: | Example Scenario |
|---|---|---|
Contain a given string in the:
| Type the string in the filter field. | You recall seeing a message containing a certain string that you want to review, but you cannot remember the exact message text. For example, type com in the filter field to display those messages that contain that string in the short message, extended message, or message ID. |
Correspond to a given message ID | Type msgid: followed by the message ID in the filter field. | You are reviewing the code that someone else wrote and you want to see the message that corresponds to a suppressed one using the %#ok<AGROW> pragma. Type msgid:agrow in the filter field. The message corresponding to AGROW is a link. Click it for more information about the message. Not all M-Lint messages have additional information. These messages do not appear as links. |
You can set using M-Lint Preferences | Click the down arrow to the right of the search field, and then click Show All . | You want, to see the complete list of messages after you have filtered the messages on a given string or filter menu option. |
Are different from the default setting (of enabled or disabled) | Click the down arrow to the right of the search field, and then click Show Messages Modified from Default. A
grey dot precedes a message with a setting different from the default.
For example:
| A coworker gave you a settings file and you want to review each message that the coworker changed from its default setting. |
Are in a given category | Click the down arrow to the right of the filter field, click Show Messages in Category, and then click the category you want. | You want to review M-Lint messages that describe coding practices that make it difficult for others to use your code. Click the down arrow to the right of the filter field, click Show Messages in Category, and then click Aesthetics and Readability. Click the messages that appear as links for more information. Not all M-Lint messages appear as links. |
Are warnings | Click the down arrow to the right of the filter field,
and choose Show All Warnings. An exclamation
point in a yellow triangle
| You recall previous warnings that your code generated, but you cannot remember enough details to use the filter field to find it. You want to skim all the warning messages to find a particular one of interest. |
Are errors | Click the down arrow to the right of the filter field,
and choose Show All Errors. By default,
an X in a red dot indicates an error message,
| You want to find a message that a script you worked on previously elicited. All you can recall is that it was an error and it involved parfor. Click the down arrow to the right of the filter field, and choose Show All Errors. Then, type a space and parfor in the filter field. The M-Lint preference panel displays only error messages that contain the word parfor. |
Are disabled | Click the down arrow to the right of the filter field, and then choose Show Disabled Messages. | You want to see the messages that M-Lint disables by default or that you have previously disabled. |
Example of Filtering Messages. To display error messages that contain the string comma and are disabled, do the following:
Click the arrow next to the filter field and select Show All Errors.
The filter field contains the string severity:error.

At the end of the string severity:error, press the Space key, and then type comma.
Click the arrow next to the filter field and select Show Disabled Messages.
The filter field now contains the string, severity:error comma enabled:false. Only the M-Lint messages that fulfill those requirements display in the Preferences panel.
To
restore the list of all M-Lint messages, click the
button.
Saving Settings. If you are likely to use different settings at different times, or if you want to make these settings available to other users, click Save As. MATLAB prompts you to provide the name of the txt file to which it will save the settings. The default location for settings is the MATLAB preferences folder (the folder returned when you run prefdir), although you can choose a different folder when saving.
Using Saved Settings. To use settings previously saved, select the settings txt file from the Active settings drop-down list. Or, select Browse to locate the settings file. Then click Apply or OK to make the settings take effect. You can also access saved settings from within the Editor using Tools > M-Lint, or the M-Lint message bar.
After selecting a settings file, you can modify the settings, but your changes automatically modify the txt file. If you want to retain the current settings in the txt file, create a copy of the settings file in which to make changes. To do so, click Save As and save the file to a different name. Then, make changes to the newly named file.
Default Settings. The Active settings indicator shows Default Settings when you are using the default settings rather than settings from a txt file. The term (modified) appears when you make changes to the default settings, but have not yet saved the changes to a file. To undo any unsaved changes and return to the default settings, click Restore Defaults. If you think you will use the modified default settings in a future session, save the settings as described in Saving Settings.
When a txt file appears in the Active settings field, return to default settings by using the drop-down list to select Default Settings.
You can switch between showing or hiding Compiler deployment messages when you work on a file by changing the M-Lint preference for this category of messages. Your choice likely depends on whether or not you are working on a file to be deployed. When you change the preference, it also changes the setting in the Editor. The converse is also true—when you change the setting from the Editor, it effectively changes this preference, however you will not see the changes reflected in the M-Lint Preferences if Preferences is open at the time you make the change. Whether or not you change the setting from the Editor or from M-Lint Preferences, it applies to the Editor and to the M-Lint Code Check Report.
To enable MATLAB Compiler deployment messages, follow these steps:
Choose File > Preferences > M-Lint.
Click the down arrow next to the filter field, and then choose Messages in Category > MATLAB Compiler (deployment) messages.
Click the Enable Category button.
Deselect individual message that you do not want M-Lint to display for your code (if any).
The settings txt file, which you can create as described in Saving Settings, includes the status of this setting.
![]() | Finding Errors, Debugging, and Correcting M-Files | Debugging Process and Features | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |