Main Content

mlint

Check MATLAB code files for possible problems

Compatibility

Note

mlint is not recommended. Use checkcode instead.

Alternatives

For information on using the graphical user interface to the Code Analyzer, see Check Code for Errors and Warnings Using the Code Analyzer.

Syntax

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','-codegen')
mlint('filename','-eml')

Description

mlint('filename') displays messages about filename that report potential problems and opportunities for code improvement. These messages are sometimes referred to as Code Analyzer messages. The line number in the message is a hyperlink that you can click to go directly to that line in the Editor. The exact text of the mlint messages is subject to some change between versions.

Specify filename as one or more character vectors or string arrays, or as a cell array of character vectors. If filename specifies multiple character vectors or string arrays, or if filename is a nonscalar string array or a cell array of character vectors, MATLAB® displays information for each file. You cannot combine cell arrays of character vectors and character vectors of file names. For example, you cannot have {'lengthofline', 'buggy'}, 'collatz' as an input.

mlint('filename','-config=settings.txt') overrides the default active settings file with the 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 Save and Reuse Code Analyzer Message Settings. 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 preference settings.

inform=mlint('filename','-struct') returns the information in a structure array whose length is the number of messages found. The structure has the fields that follow.

Field

Description

message

Message describing the suspicious construct that code analysis caught.

line

Vector of file line numbers to which the message refers.

column

Two-column array of file columns (column extents) to which the message applies. The first column of the array specifies the column in the Editor where the message begins. The second column of the array specifies the column in the Editor where the message ends. There is one row in the two-column array for each occurrence of a message.

If you specify multiple file names as input, inform contains a cell array of structures.

msg=mlint('filename','-string') returns the information as the character vector msg. If you specify multiple file names as input, msg contains information for each file, separated by 10 equal sign characters (=), a space, the file name, a space, and 10 equal sign characters.

If you omit the -struct or -string argument and you specify an output argument, the default behavior is -struct. If you omit the argument 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 you specified them.

inform=mlint('filename','-id') requests the message ID, where ID is a character vector 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 mlint 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 directive, %#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','-codegen') enables code generation messages for display in the Command Window.

mlint('filename','-eml') '-eml' is not recommended. Use '-codegen' instead.

Examples

The following examples use lengthofline.m, which is a sample file with MATLAB code that can be improved. You can find it in matlabroot/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.

Running mlint on a File with No Options

To run mlint on the example file, lengthofline.m, run

mlint('lengthofline')

MATLAB displays the M-Lint messages for lengthofline.m 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 Change Code Based on Code Analyzer Messages in the MATLAB Desktop Tools and Development Environment documentation.

Running mlint with Options to Show IDs and Return Results to a Structure

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
    id

To 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, the message is for the value that appears on line 22 that extends from column 1–9 in the file.NASGU is the ID for the message 'The value assigned here to variable 'nothandle' might never be used.'.

Displaying McCabe Complexity with mlint

To display the McCabe complexity of a MATLAB code file, run mlint with the -cyc option, as shown in the following example (assuming you have saved lengthofline.m to a local folder).

mlint lengthofline.m -cyc

Results displayed in the Command Window show the McCabe complexity of the file, followed by the M-Lint messages, as shown here:

L 1 (C 23-34): The McCabe complexity of 'lengthofline' is 12.
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 UPPER/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.

See Also

mlintrpt, profile

How To

Extended Capabilities

Version History

Introduced before R2006a