Check MATLAB code files for possible problems
checkcode(
displays
messages about filename
)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 checkcode
messages
is subject to some change between versions.
returns
the information as an info
= checkcode(___,'-struct')n
-by-1
structure
array, where n
is the number of messages found.
returns
the information as a character vector.msg
= checkcode(___,'-string')
If you omit the '-struct'
or '-string'
argument
and you specify an output argument, the default behavior is '-struct'
.
[___,
also returns filepaths
]
= checkcode(___)filepaths
,
the absolute paths to the file names. You can specify filepaths
with
either the '-struct'
or '-string'
options.
Run checkcode
on the example file lengthofline.m
. MATLAB® displays the Code Analyzer messages for lengthofline.m
in the Command Window.
checkcode('lengthofline')
L 21 (C 1-9): The value assigned to variable 'nothandle' might be unused. L 22 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)). L 23 (C 5-11): The variable 'notline' appears to change size on every loop iteration. Consider preallocating for speed. L 23 (C 44-49): Use STRCMPI(str1,str2) instead of using UPPER/LOWER in a call to STRCMP. L 27 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)). L 33 (C 13-16): The variable 'data' appears to change size on every loop iteration. Consider preallocating for speed. L 33 (C 24-31): Use dynamic fieldnames with structures instead of GETFIELD. L 37 (C 29): Use || instead of | as the OR operator in (scalar) conditional statements. L 38 (C 47): 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 42 (C 13-15): The variable 'dim' appears to change size on every loop iteration. Consider preallocating for speed. L 44 (C 13-15): The variable 'dim' appears to change size on every loop iteration. Consider preallocating for speed. L 47 (C 52): Invalid syntax at ';'. Possibly, a ), }, or ] is missing. L 47 (C 53): Invalid syntax at ')'. Possibly, a ), }, or ] is missing. L 47 (C 54): Parse error at ']': usage might be invalid MATLAB syntax. L 48 (C 17): Terminate statement with semicolon to suppress output (in functions). L 48 (C 23): Use of brackets [] is unnecessary. Use parentheses to group, if needed.
Run checkcode
on the example file lengthofline.m
. Include message IDs and store the results in a structure.
info = checkcode('lengthofline', '-id')
info = 17×1 struct array with fields: id message fix line column
View the values for the first message
info(1)
ans = struct with fields: id: 'NASGU' message: 'The value assigned to variable 'nothandle' might be unused.' fix: 0 line: 21 column: [1 9]
Run checkcode
on the example file lengthofline.m
using the '-cyc'
option. MATLAB® displays the McCabe complexity of the file, followed by the Code Analyzer messages for lengthofline.m
.
checkcode('lengthofline', '-cyc')
L 1 (C 23-34): The McCabe complexity of 'lengthofline' is 12. L 21 (C 1-9): The value assigned to variable 'nothandle' might be unused. L 22 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)). L 23 (C 5-11): The variable 'notline' appears to change size on every loop iteration. Consider preallocating for speed. L 23 (C 44-49): Use STRCMPI(str1,str2) instead of using UPPER/LOWER in a call to STRCMP. L 27 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)). L 33 (C 13-16): The variable 'data' appears to change size on every loop iteration. Consider preallocating for speed. L 33 (C 24-31): Use dynamic fieldnames with structures instead of GETFIELD. L 37 (C 29): Use || instead of | as the OR operator in (scalar) conditional statements. L 38 (C 47): 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 42 (C 13-15): The variable 'dim' appears to change size on every loop iteration. Consider preallocating for speed. L 44 (C 13-15): The variable 'dim' appears to change size on every loop iteration. Consider preallocating for speed. L 47 (C 52): Invalid syntax at ';'. Possibly, a ), }, or ] is missing. L 47 (C 53): Invalid syntax at ')'. Possibly, a ), }, or ] is missing. L 47 (C 54): Parse error at ']': usage might be invalid MATLAB syntax. L 48 (C 17): Terminate statement with semicolon to suppress output (in functions). L 48 (C 23): Use of brackets [] is unnecessary. Use parentheses to group, if needed.
Suppress specific messages by creating and specifying a settings file. For example, the file lengthofline.m
includes several lines that use | instead of || as the OR
operator. By default, checkcode
flags these lines.
checkcode('lengthofline')
L 21 (C 1-9): The value assigned to variable 'nothandle' might be unused. L 22 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)). L 23 (C 5-11): The variable 'notline' appears to change size on every loop iteration. Consider preallocating for speed. L 23 (C 44-49): Use STRCMPI(str1,str2) instead of using UPPER/LOWER in a call to STRCMP. L 27 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)). L 33 (C 13-16): The variable 'data' appears to change size on every loop iteration. Consider preallocating for speed. L 33 (C 24-31): Use dynamic fieldnames with structures instead of GETFIELD. L 37 (C 29): Use || instead of | as the OR operator in (scalar) conditional statements. L 38 (C 47): 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 42 (C 13-15): The variable 'dim' appears to change size on every loop iteration. Consider preallocating for speed. L 44 (C 13-15): The variable 'dim' appears to change size on every loop iteration. Consider preallocating for speed. L 47 (C 52): Invalid syntax at ';'. Possibly, a ), }, or ] is missing. L 47 (C 53): Invalid syntax at ')'. Possibly, a ), }, or ] is missing. L 47 (C 54): Parse error at ']': usage might be invalid MATLAB syntax. L 48 (C 17): Terminate statement with semicolon to suppress output (in functions). L 48 (C 23): Use of brackets [] is unnecessary. Use parentheses to group, if needed.
Create a settings file that suppresses the message flagging the use of | as the OR
operator.
On the Home tab, in the Environment section, click the Preferences button.
Select Code Analyzer in the left pane.
Under Default Settings, in the Aesthetics and Readability section, clear the message Use || instead of | as the OR operator in (scalar) conditional statements.
Enter mysettings.txt
as the file name and save it to your current folder.
Press the Cancel button to the preference panel without changing the active settings.
Run checkcode
on the example file using the custom settings file mysettings.txt
. The message Use || instead of | as the OR operator in (scalar) conditional statements is suppressed and is no longer visible in the list of messages.
checkcode('lengthofline','-config=mysettings.txt')
L 21 (C 1-9): The value assigned to variable 'nothandle' might be unused. L 22 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)). L 23 (C 5-11): The variable 'notline' appears to change size on every loop iteration. Consider preallocating for speed. L 23 (C 44-49): Use STRCMPI(str1,str2) instead of using UPPER/LOWER in a call to STRCMP. L 27 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)). L 33 (C 13-16): The variable 'data' appears to change size on every loop iteration. Consider preallocating for speed. L 33 (C 24-31): Use dynamic fieldnames with structures instead of GETFIELD. L 42 (C 13-15): The variable 'dim' appears to change size on every loop iteration. Consider preallocating for speed. L 44 (C 13-15): The variable 'dim' appears to change size on every loop iteration. Consider preallocating for speed. L 47 (C 52): Invalid syntax at ';'. Possibly, a ), }, or ] is missing. L 47 (C 53): Invalid syntax at ')'. Possibly, a ), }, or ] is missing. L 47 (C 54): Parse error at ']': usage might be invalid MATLAB syntax. L 48 (C 17): Terminate statement with semicolon to suppress output (in functions). L 48 (C 23): Use of brackets [] is unnecessary. Use parentheses to group, if needed.
filename
— File nameFile name, specified as a character vector or a cell array of character vectors. The file name can include a partial path, but must be in a folder on the search path or in the current folder.
If filename
is a cell array, MATLAB® displays
information for each file.
Note:
You cannot combine cell arrays and character arrays of file
names. For example, you cannot have |
Example: 'lengthofline'
Example: {'lengthofline', 'buggy'}
option
— Display option'-id'
| '-fullpath'
| '-notok'
| '-cyc'
| '-config'
Display option, specified as one of these values. Options can appear in any order.
Option | Description |
---|---|
'-id' | Request the message ID, where ID is a character vector. When
returned to a structure, the output also has the id field,
which is the ID associated with the message. |
'-fullpath' | Assume that the input file names are absolute paths, so that checkcode does
not try to locate them. |
'-notok' | Run For
information on |
'-cyc' | Display the McCabe complexity (also referred to as cyclomatic complexity) of each function in the file. In general, lower complexity values indicate programs that are easier to understand and modify. Evidence suggests 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. Some people advocate splitting up programs that have a complexity value over 10. |
| Override the default active settings file with the specified settings file. If the specified file is not in the current folder, provide the full path to the file. For
information about creating a settings file, see Save and Reuse Code Analyzer Message Settings. If
you specify an invalid file, To ignore all settings files and use the factory
default preference settings, specify |
info
— Message informationMessage information, returned as a n
-by-1
structure
array, where n
is the number of messages returned
by the checkcode
command. If you specify multiple
file names as input, or if you specify a cell array as input, info
contains
a cell array of structures.
Field | Description |
---|---|
| Message describing the suspicious construct that code analysis caught. |
| Vector of line numbers, indicating which lines of the file the message applies to. |
| Two-column array of columns numbers (column extents), indicating which columns of the file the message applies to. 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. In the two-column array, each occurrence of a message has a row. |
msg
— Message informationMessage information, returned as a character vector. If you
specify multiple file names as input, or if you specify a cell array
as input, msg
contains a character vector where
the information for each file is separated by 10 equal sign characters,
a space, the file name, a space, and 10 equal sign characters.
Example: ========== C:\MyMatlabFiles\buggy.m ==========
filepaths
— Absolute paths of filesAbsolute paths of files, specified as a cell array of character
vectors. MATLAB lists the filepaths
in the
same order as the specified input files.
To force the Code Analyzer to ignore a line of code, use %#ok
at
the end of the line. You can add comments after the tag.
unsuppressed1 = 10 % This line will get caught suppressed2 = 20 %#ok This line will not get caught suppressed3 = 30 %#ok This line will not get caught