checkcode - Check MATLAB code files for possible problems - MATLAB (original) (raw)
Check MATLAB code files for possible problems
Syntax
Description
Note
In R2022b: codeIssues is recommended overcheckcode
because it features improved interactivity and the ability to store identified issues.
checkcode([filename](#bs2y5lo-1-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 thecheckcode
messages is subject to some change between versions.
checkcode([filename](#bs2y5lo-1-filename)1,...,[filename](#bs2y5lo-1-filename)N)
displays messages for each specified filename
.
checkcode(___,[option](#bs2y5lo-1-option)1,...,[option](#bs2y5lo-1-option)N)
modifies the returned messages based on the specified option flags. For example, specify '-modcyc'
to request the modified cyclomatic complexity to be returned with each message. You can specify options with any of the input arguments in the previous syntaxes.
[info](#bs2y5lo-1-info) = checkcode(___,'-struct')
returns the information as an n
-by-1
structure array, where n
is the number of messages found.
[msg](#bs2y5lo-1-msg) = checkcode(___,'-string')
returns the information as a character vector.
If you omit the '-struct'
or '-string'
argument and you specify an output argument, the default behavior is '-struct'
.
[___, [filepaths](#bs2y5lo-1-filepaths)] = checkcode(___)
also returns filepaths
, the absolute paths to the file names. You can specify filepaths
with either the '-struct'
or '-string'
options.
Examples
Check for Potential Problems in File
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): Value assigned to variable might be unused. L 22 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)). L 23 (C 5-11): Variable 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): Variable 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 38 (C 17-45): To improve performance, use 'isscalar' instead of length comparison. L 39 (C 17-45): To improve performance, use 'isscalar' instead of length comparison. L 40 (C 17-45): To improve performance, use 'isscalar' instead of length comparison. L 42 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed. L 44 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed. L 47 (C 21): A '[' might be missing a closing ']', causing invalid syntax at ')'. L 47 (C 51): A '(' might be missing a closing ')', causing invalid syntax at ';'. L 47 (C 54): Parse error at ']': usage might be invalid MATLAB syntax. L 48 (C 17): Add a semicolon after the statement to hide the output (in a function).
Store List of Potential Problems
Run checkcode
on the example file lengthofline.m
. Include message IDs and store the results in a structure.
info = checkcode('lengthofline', '-id')
info=16×1 struct array with fields: id message fix line column
View the values for the first message.
ans = struct with fields: id: 'NASGU' message: 'Value assigned to variable might be unused.' fix: 0 line: 21 column: [1 9]
Display the Modified Cyclomatic Complexity of File
Run checkcode
on the example file lengthofline.m
using the '-modcyc'
option. MATLAB® displays the modified cyclomatic complexity of the file, followed by the Code Analyzer messages for lengthofline.m
.
checkcode('lengthofline', '-modcyc')
L 1 (C 23-34): The modified cyclomatic complexity of 'lengthofline' is 12. L 21 (C 1-9): Value assigned to variable might be unused. L 22 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)). L 23 (C 5-11): Variable 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): Variable 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 38 (C 17-45): To improve performance, use 'isscalar' instead of length comparison. L 39 (C 17-45): To improve performance, use 'isscalar' instead of length comparison. L 40 (C 17-45): To improve performance, use 'isscalar' instead of length comparison. L 42 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed. L 44 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed. L 47 (C 21): A '[' might be missing a closing ']', causing invalid syntax at ')'. L 47 (C 51): A '(' might be missing a closing ')', causing invalid syntax at ';'. L 47 (C 54): Parse error at ']': usage might be invalid MATLAB syntax. L 48 (C 17): Add a semicolon after the statement to hide the output (in a function).
Suppress Code Analyzer Messages
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): Value assigned to variable might be unused. L 22 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)). L 23 (C 5-11): Variable 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): Variable 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 38 (C 17-45): To improve performance, use 'isscalar' instead of length comparison. L 39 (C 17-45): To improve performance, use 'isscalar' instead of length comparison. L 40 (C 17-45): To improve performance, use 'isscalar' instead of length comparison. L 42 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed. L 44 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed. L 47 (C 21): A '[' might be missing a closing ']', causing invalid syntax at ')'. L 47 (C 51): A '(' might be missing a closing ')', causing invalid syntax at ';'. L 47 (C 54): Parse error at ']': usage might be invalid MATLAB syntax. L 48 (C 17): Add a semicolon after the statement to hide the output (in a function).
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 exit out of 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): Value assigned to variable might be unused. L 22 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)). L 23 (C 5-11): Variable 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): Variable 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 38 (C 17-45): To improve performance, use 'isscalar' instead of length comparison. L 39 (C 17-45): To improve performance, use 'isscalar' instead of length comparison. L 40 (C 17-45): To improve performance, use 'isscalar' instead of length comparison. L 42 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed. L 44 (C 13-15): Variable appears to change size on every loop iteration. Consider preallocating for speed. L 47 (C 21): A '[' might be missing a closing ']', causing invalid syntax at ')'. L 47 (C 51): A '(' might be missing a closing ')', causing invalid syntax at ';'. L 47 (C 54): Parse error at ']': usage might be invalid MATLAB syntax. L 48 (C 17): Add a semicolon after the statement to hide the output (in a function).
Input Arguments
filename
— File name
character vector | string array | cell array of character vectors
File name, specified as a character vector, a string array, 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 nonscalar string array or a cell array of character vectors, MATLAB® displays information for each file.
Note
You cannot combine cell arrays and character arrays of file names. For example, you cannot have {'lengthofline', 'buggy'}, 'collatz'
as an input.
Example: 'lengthofline'
Example: {'lengthofline', 'buggy'}
Data Types: char
| string
option
— Display option
'-id'
| '-fullpath'
| '-notok'
| '-cyc'
| '-modcyc'
| '-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 checkcode for all lines in filename, even those lines that end with thecheckcode suppression directive,%#ok.For information on %#ok and suppressing messages from within your program, see Adjust Code Analyzer Message Indicators and Messages. |
'-cyc' | Display the McCabe 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.For more information on cyclomatic complexity, see Measure Code Complexity Using Cyclomatic Complexity. |
'-modcyc' | Displays the modified cyclomatic complexity of each function in the file. The modified cyclomatic complexity for a function is equal to the McCabe cyclomatic complexity except for one difference. McCabe cyclomatic complexity counts each individualcase within aswitch statement as 1, while modified cyclomatic complexity counts the entireswitch statement as 1. In general, switch statements are simpler than nested if-elseif-else statements and therefore, the modified cyclomatic complexity is often considered a better measure of code complexity.For more information on cyclomatic complexity, see Measure Code Complexity Using Cyclomatic Complexity. |
'-config=settingsfile''-config=factory' | 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,checkcode returns a message indicating that it cannot open or read the file you specified. In that case, checkcode uses the factory default settings.To ignore all settings files and use the factory default preference settings, specify'-config=factory'. |
Output Arguments
info
— Message information
structure array | cell array
Message 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 | Message describing the suspicious construct that code analysis caught. |
line | Vector of line numbers, indicating which lines of the file the message applies to. |
column | 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 information
character vector
Message 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 files
cell array of character vectors
Absolute paths of files, specified as a cell array of character vectors. MATLAB lists the filepaths
in the same order as the specified input files.
Tips
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
Extended Capabilities
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced in R2011b