| MATLAB® | ![]() |
| On this page… |
|---|
Running the M-Lint Code Check Directory Report |
The M-Lint Code Check Report displays potential errors and problems, as well as opportunities for improvement in your code. The term "lint" is the name given to similar tools used with other programming languages such as C. M-Lint produces a message for each line of an M-file that it determines might be improved. For example, a common M-Lint message is that a variable foo in line 12 is defined but never used in the M-file.
To run the M-Lint code check directory report, follow these steps:
In the Current Directory browser, navigate to the directory that contains the M-files you want to check with M-Lint. To use the example shown in this documentation, lengthofline.m, you can change the current directory by running
cd(fullfile(matlabroot,'help','techdoc','matlab_env','examples'))
If you plan to modify the example, save the file to a directory for which you have write access, and then make that directory the current MATLAB® directory. In this example, the file is saved to I:\MATLABFiles\mymfiles.
In the Current Directory browser toolbar, select the M-Lint Code Check Report from the Directory Reports listing—for details, see Accessing and Using Directory Reports.
The M-Lint Code Check Report displays in the MATLAB Web Browser, showing those M-files that M-Lint identified as having potential problems or opportunities for improvement.

For each message, review the suggestion and your code, click the line number to open the M-file in the Editor at that line, and make changes based on the message. Use the following general advice:
If you are not sure what a message means or what to change in the code as a result, use the Help browser to look for related topics in the online documentation. For examples of messages and what to do about them, including specific changes to make for the example, lengthofline.m, see Making Changes Based on M-Lint Messages.
M-Lint does not provide perfect information about every situation and in some cases, you might not want to make any changes based on the 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 message for that line in the M-Lint Report, instruct M-Lint to ignore a line by adding %#ok to the end of the line in the M-file. (You can override the %#ok by running the mlint function with the '-notok' tag.)
If there are certain messages or types of messages you do not want to see, you can set a preference so that M-Lint does not report them. Select File > Preferences > M-Lint. In Select messages to enable, clear the check box for messages you do not want to see. Review the settings for all messages to ensure you are seeing those pertinent to your file. Click OK. For more information, click the Help button in the M-Lint Preferences pane. The next time you run the report, the messages will not appear. You can use %#ok with a specific message ID so that only that type of message is suppressed—for more information, see the reference page for mlint.
After making changes, save the M-file. Consider saving the file to a different name if you made significant changes that might introduce errors. Then you can refer to the original file if needed to resolve problems with the updated file. Use Tools > Compare Against in the Editor to help you identify the changes you made to the file. For more information, see Comparing Files and Directories.
Run and debug the file(s) again to be sure you have not introduced any inadvertent errors.
If the M-Lint Code Check Report is already displayed, click Rerun This Report to update the report based on the changes you made to the file, or run the report from the Current Directory browser toolbar. Ensure the M-Lint messages are gone, based on the changes you made to the M-files.
For information on how to correct the potential problems presented by M-Lint, use the following resources:
Look for relevant topics in the MATLAB Programming and Programming Tips documentation.
Use the Help browser Search and Index panes to find documentation about terms presented in the M-Lint messages.
Other techniques to help you identify problems in and improve your M-files are in these topics:
Syntax Highlighting in the Command Window and the Editor
Examining Errors generated when you run the M-file
Finding Errors, Debugging, and Correcting M-Files, namely the Editor and debugging functions
Profiling for Improving Performance for improving performance
An example file, lengthofline.m, is included with the MATLAB product in matlabroot/matlab/help/techdoc/matlab_env/examples.
To run the M-Lint Code Check Report for lengthofline.m, change the current directory to its location by running
cd(fullfile(matlabroot,'help','techdoc','matlab_env','examples'))
In the Current Directory browser, select the M-Lint Code Check Report from the list of directory reports on the toolbar.
The M-Lint Code Check Report appears, with its list of messages suggesting improvements you can make to lengthofline.m and any other files in the directory.

Messages and Resulting Changes for the lengthofline Example. The following table describes each message and demonstrates a way to change the file, based on the message.
Message — Code (Original Line Numbers) | Explanation and Updated Code (New Line Numbers) |
|---|---|
22: The value assigned here to variable 'nothandle' might never be used. — — — — — — — — — — — — — — — — — 22 nothandle = ~ishandle(hline); | In line 22, nothandle is assigned a value, but nothandle 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, which is the case for the lengthofline example. Update line 24 to use nothandle, which is faster than computing ~ishandle for each iteration of the loop, as shown here. |
23: NUMEL(x) is usually faster than PROD(SIZE(x)). — — — — — — — — — — — — — — — — — | While prod(size(x)) returns the number of elements in a matrix, the numel function was designed to do just that, and therefore is usually more efficient. Type doc numel to see the numel reference page. Change the line to 23 for nh = 1:numel(hline) |
24: 'notline' might be growing inside a loop. Consider preallocating for speed. — — — — — — — — — — — — — — — — — | When you increase the size of an array within a loop, it is inefficient. Before the loop, preallocate the array to its maximum size to improve performance. For more information, see Preallocating Memory in the MATLAB Programming documentation. In the example, add a new line to preallocate notline before the loop. |
24: Use STRCMPI(str1,str2) instead of using LOWER in a call to STRCMP. — — — — — — — — — — — — — — — — — 24 notline(nh)=~ishandle(hline(nh)) || ~strcmp('line',lower(get(hline(nh), 'type'))); | While strcmp ('line',lower(get(hline(nh)'type')) converts the result of the get function to a lowercase string before doing the comparison, the strcmpi function ignores the case while performing the comparison, with advantages that include more efficiency. Change line 25 to notline(nh) = nothandle(nh) || ~strcmpi('line',get(hline(nh),'type')); |
28: NUMEL(x) is usually faster than PROD(SIZE(x)). — — — — — — — — — — — — — — — — — | See the same message and explanation reported for line 23. Change the line 29 to |
34: 'data' might be growing inside a loop. Consider preallocating for speed. — — — — — — — — — — — — — — — — — | See the same message and explanation reported for line 24. Add this line, 34, before the loop |
34: Use dynamic fieldnames with structures instead of GETFIELD. Type 'doc struct' for more information. — — — — — — — — — — — — — — — — — | You can access a field in a structure as a variable expression that MATLAB evaluates at run-time. This is more efficient than using getfield. For more information, type doc struct to see the reference page for structures, or see Using Dynamic Field Names in the MATLAB Programming documentation. Change line 37 to |
38: Use || instead of | as the OR operator in (scalar) conditional statements. 39: Use || instead of | as the OR operator in (scalar) conditional statements. 40: Use || instead of | as the OR operator in (scalar) conditional statements. — — — — — — — — — — — — — — — — — 39 (length(unique(data{1}(:)))==1 | ... | While | (the element-wise logical OR operator) performs the comparison correctly, use the || (short circuit OR operator) for efficiency. For details, see Logical Operators in the MATLAB Programming documentation. Change lines 40, 41, and 42 to |
42: 'data' might be growing inside a loop. Consider preallocating for speed. — — — — — — — — — — — — — — — — — 42 data{3} = zeros(size(data{1})); | This message no longer appears due to the change made to line 34 data{nd} = getfield(flds,fdata{nd});. Sometimes fixing code in one line automatically clears a message for another line. If the reason for a message or the action to take for a message is not obvious at first, it could be because another line is causing the message. Address the issues that are easy to fix first and rerun the report. Do not make any changes to line 44. |
43: 'dim' might be growing inside a loop. Consider preallocating for speed. | See the same message and explanation reported for line 24. Add this line before the first line of the loop |
48: There may be a parenthesis imbalance around here. 48: There may be a parenthesis imbalance around here. 48: There may be a parenthesis imbalance around here. 48: There may be a parenthesis imbalance around here. | There is an error in this line, which you would see by running lengthofline. M-Lint suggests that it might be due to a parenthesis imbalance. You can check that by moving the arrow key over each of the delimiters, to see if MATLAB 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 51, change data{3}(;) to data{3}(:). That single change addressed the issues in all the messages for that line. |
49: 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 this line, do not add the semicolon. You can instruct M-Lint to ignore all messages on this line so that the messages on it will not appear by adding %#ok to the end of the line. However, because there is currently another message on the line, do not add %#ok until you have addressed the other message. Alternatively, you can add %#ok with the message ID for the specific message you want to suppress. To determine the message ID, run mlint('lengthofline.m', '-id'), which indicates the ID is NOPRT—for more information, see the mlint function reference page. For this example, assume you want to display the output and suppress the M-Lint message. To do so, add %#ok<NOPRT> to the end of the line. Note that there is a similar message for M-file scripts. This is so you can suppress the message for M-files that are cell-mode scripts, because they are often intended as demos and the display of output is intentional. |
49: Use of brackets [] is unnecessary. Use parentheses to group, if needed. — — — — — — — — — — — — — — — — — | For more information about the use of brackets and parentheses, see the Special Characters reference page. In this example, remove the brackets because they are not needed. They add processing time because MATLAB concatenates unnecessarily. Change line 52 to |

The M-file that includes all of the changes suggested by M-Lint is lengthofline2.m. To view it, run
edit(fullfile(matlabroot,'help','techdoc','matlab_env',... 'examples','lengthofline2.m')).
You can get M-Lint messages using any of the following methods. Each provides the same M-Lint messages, but in a different format:
Access the M-Lint Code Check report for an M-file from the Editor Tools menu or from the Profiler detail report.
Run the mlint function, which analyzes the specified file and displays messages in the Command Window, or mlintrpt, which runs mlint and displays the messages in the Web Browser.
Use automatic M-Lint analysis and code correction while you work on a file in the Editor—see M-Lint Code Analyzer.
![]() | Directory Reports in Current Directory Browser | Profiling for Improving Performance | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |