checkcode
Check MATLAB code files for possible problems
Syntax
Description
Note
In R2022b: codeIssues
is recommended over
checkcode
because it features improved
interactivity and the ability to store identified issues.
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.
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.
info(1)
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 For information
on |
'-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 individual
For more information on cyclomatic complexity, see Measure Code Complexity Using Cyclomatic Complexity. |
| 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
|
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 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 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
MATLAB-Befehl
Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:
Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)