| Contents | Index |
Note mlint is not recommended. Use checkcode instead. |
For information on using the graphical user interface to the Code Analyzer, see Avoid Mistakes While Editing Code.
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')
mlint('filename') displays Code Analyzer messages about filename, where the message reports potential problems and opportunities for code improvement. 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 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.
If you specify multiple file names as input, or if you specify a cell array as input, inform contains a cell array of structures.
msg=mlint('filename','-string') returns the information as a string to the variable msg. If you specify multiple file names as input, or if you specify a cell array as input, msg contains 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 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 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 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.
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.
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 Changing Code Based on Code Analyzer 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, 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.'.
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.
For information on the suppression directive, %#ok, and suppressing messages from within your program, see Adjust Code Analyzer Message Indicators and Messages.
For information on enabling code generation messages from within your program, see Adding the Compilation Directive %#codegen.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |