| MATLAB Function Reference | ![]() |
From the Current Directory browser, select View > Directory Reports > M-Lint Code Check Report on the menu bar. See also the automatic M-Lint Code Analyzer in the Editor.
mlint('filename')
mlint('filename','-config=settings.txt')
mlint('filename','-config=factory')
inform=mlint('filename','-struct')
msg=mlint('filename','-string')
[inform,filepaths]=mlint('filename')
inform=mlint('filename','-id')
inform=mlint('filename','-fullpath')
inform=mlint('filename','-notok'
mlint('filename','-cyc')
mlint('filename','-eml')
%#ok
%#eml
mlint('filename') displays M-Lint information about filename, where the information reports potential problems and opportunities for code improvement, referred to as suspicious constructs. The line number in the message is a hyperlink that opens the file in the Editor, scrolled to that line. If filename is a cell array, information is displayed for each file. For mlint(F1,F2,F3,...), where each input is a character array, MATLAB® software displays information about each input file name. You cannot combine cell arrays and character arrays of file names. Note that the exact text of the mlint messages is subject to some change between versions.
mlint('filename','-config=settings.txt') overrides the default M-lint active settings file with the M-Lint settings that enable or suppress messages as indicated in the specified settings.txt file.
Note If used, you must specify the full path to the settings.txt file specified with the -config option. |
For information about creating a settings.txt file, see the information on Preferences for M-Lint. If you specify an invalid file, mlint returns a message indicating that it cannot open or read the file you specified. In that case, mlint uses the factory default settings.
mlint('filename','-config=factory') ignores all settings files and uses the factory default M-lint preference settings.
inform=mlint('filename','-struct') returns the M-Lint information in a structure array whose length is the number of suspicious constructs found. The structure has the following fields:
Field | Description |
|---|---|
Two-column array of columns to which the message applies, for each line | |
Message describing the suspicious construct that M-Lint caught |
If multiple file names are input, or if a cell array is input, inform will contain a cell array of structures.
msg=mlint('filename','-string') returns the M-Lint information as a string to the variable msg. If multiple file names are input, or if a cell array is input, msg will contain a string where each file's information is separated by 10 equal sign characters (=), a space, the file name, a space, and 10 equal sign characters.
If the -struct or -string argument is omitted and an output argument is specified, the default behavior is -struct. If the argument is omitted and there are no output arguments, the default behavior is to display the information to the command line.
[inform,filepaths]=mlint('filename') additionally returns filepaths, the absolute paths to the file names, in the same order as they were input.
inform=mlint('filename','-id') requests the message ID from M-Lint, where ID is a string of the form ABC.... When returned to a structure, the output also has the id field, which is the ID associated with the message.
inform=mlint('filename','-fullpath') assumes that the input file names are absolute paths, so that M-Lint does not try to locate them.
inform=mlint('filename','-notok') runs mlint for all lines in filename, even those lines that end with the mlint suppression syntax, %#ok.
mlint('filename','-cyc') displays the McCabe complexity (also referred to as cyclomatic complexity) of each function in the file. Higher McCabe complexity values indicate higher complexity, and there is some evidence to suggest that programs with higher complexity values are more likely to contain errors. Frequently, you can lower the complexity of a function by dividing it into smaller, simpler functions. In general, smaller complexity values indicate programs that are easier to understand and modify. Some people advocate splitting up programs that have a complexity rating over 10.
mlint('filename','-eml') enables Embedded MATLAB™ messages for display in the Command Window.
%#ok at the end of a line in an M-file causes mlint to ignore those lines in the file. MATLAB comments can follow the %#ok pragma . mlint ignores specified messages 1 through n when %#ok<id1,id2,...idn> appears at the end of the line.
%#eml anywhere within an M-file, except within a comment, causes mlint to enable Embedded MATLAB messages in the Embedded MATLAB Editor. See Adding the Compilation Directive %#eml for more information. MATLAB comments can follow the %#eml pragma.
The following examples use lengthofline.m, which is an example M-file with code that can be improved. You can find it in matlabroot/matlab/help/techdoc/matlab_env/examples. If you want to run the examples, save a copy of lengthofline.m to a location on your MATLAB path.
To run mlint on the example file, lengthofline, run
mlint('lengthofline'))
MATLAB displays M-Lint messages for lengthofline in the Command Window:
L 22 (C 1-9): The value assigned here to variable 'nothandle' might never be used.
L 23 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).
L 24 (C 5-11): 'notline' might be growing inside a loop. Consider preallocating for speed.
L 24 (C 44-49): Use STRCMPI(str1,str2) instead of using LOWER in a call to STRCMP.
L 28 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).
L 34 (C 13-16): 'data' might be growing inside a loop. Consider preallocating for speed.
L 34 (C 24-31): Use dynamic fieldnames with structures instead of GETFIELD.
Type 'doc struct' for more information.
L 38 (C 29): Use || instead of | as the OR operator in (scalar) conditional statements.
L 39 (C 47): Use || instead of | as the OR operator in (scalar) conditional statements.
L 40 (C 47): Use || instead of | as the OR operator in (scalar) conditional statements.
L 42 (C 13-16): 'data' might be growing inside a loop. Consider preallocating for speed.
L 43 (C 13-15): 'dim' might be growing inside a loop. Consider preallocating for speed.
L 45 (C 13-15): 'dim' might be growing inside a loop.Consider preallocating for speed.
L 48 (C 52): There may be a parenthesis imbalance around here.
L 48 (C 53): There may be a parenthesis imbalance around here.
L 48 (C 54): There may be a parenthesis imbalance around here.
L 48 (C 55): There may be a parenthesis imbalance around here.
L 49 (C 17): Terminate statement with semicolon to suppress output (in functions).
L 49 (C 23): Use of brackets [] is unnecessary. Use parentheses to group, if needed.For details about these messages and how to improve the code, see Making Changes Based on M-Lint Messages in the MATLAB Desktop Tools and Development Environment documentation.
To store the results to a structure and include message IDs, run
inform=mlint('lengthofline', '-id')
MATLAB returns
inform =
19x1 struct array with fields:
message
line
column
idTo see values for the first message, run
inform(1)
MATLAB displays
ans =
message: 'The value assigned here to variable 'nothandle' might never be used.'
line: 22
column: [1 9]
id: 'NASGU'Here, NASGU is the ID for the message 'The value assigned here to variable 'nothandle' might never be used.'.
This examples shows how to instruct mlint to ignore lines, where these are lines in the example M-file, lengthofline:
22 nothandle = ~ishandle(hline);
The M-Lint message is
L 22 (C 1-9): The value assigned here to variable 'nothandle' might never be used.
To suppress the message, add %#ok to the end of line 22 in the M-file (assuming you have saved lengthofline.m to a local directory).
22 nothandle = ~ishandle(hline); %#ok
When you run mlint for lengthofline, no messages are shown for line 22 because it contains the %#ok message suppression syntax.
When you add %#ok to a line, it suppresses all mlint messages for that line. If there are multiple messages in a line and you want to suppress some but not all of them, or if you want to suppress a specific message but not all messages that might arise in the future due to changes you make, use the %#ok syntax in conjunction with message IDs.
Run mlint with the -id option:
mlint('lengthofline','-id')
Results displayed to the Command Window show two messages for line 34:
L 34 (C 13-16): AGROW: 'data' might be growing inside a loop.
Consider preallocating for speed.
L 34 (C 24-31): GFLD: Use dynamic fieldnames with structures instead of GETFIELD.
Type 'doc struct' for more information.
To suppress only the first message about 'data' growing inside a loop, use its message ID, GFLD, with the %#ok syntax as shown here:
data{nd} = getfield(flds,fdata{nd}); %#ok<GFLD>
When you run mlint for lengthofline, only one message now displays for line 34.
To display multiple specific messages for a line, separate message IDs with commas in the %#ok syntax:
data{nd} = getfield(flds,fdata{nd}); %#ok<GFLD,AGROW>
Now when you run mlint for lengthofline, no messages display for line 34.
To display the McCabe complexity of an M-File, run mlint with the -cyc option, as shown in the following example (assuming you have saved lengthofline.m to a local directory).
mlint('lengthofline.m', '-cyc')
Results displayed in the Command Window show the McCabe complexity of the file, followed by the M-File messages, as shown here:
L 1 (C 14-21): The McCabe complexity of 'lengthofline' is 12. L 33 (C 18): Use || instead of | as the OR operator in (scalar) conditional statements. L 34 (C 7): 'f' might be growing inside a loop. Consider preallocating for speed. L 37 (C 23): Use && instead of & as the AND operator in (scalar) conditional statements. L 38 (C 10): 'f' might be growing inside a loop. Consider preallocating for speed. L 39 (C 27): Use || instead of | as the OR operator in (scalar) conditional statements. L 39 (C 42): Use || instead of | as the OR operator in (scalar) conditional statements. L 39 (C 51): Use && instead of & as the AND operator in (scalar) conditional statements. L 39 (C 66): Use || instead of | as the OR operator in (scalar) conditional statements. L 40 (C 10): 'f' might be growing inside a loop. Consider preallocating for speed. L 42 (C 10): 'f' might be growing inside a loop. Consider preallocating for speed.
![]() | mldivide \, mrdivide / | mlintrpt | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |