Main Content

checkcode

Check MATLAB code files for possible problems

Description

example

Note

In R2022b: codeIssues is recommended over checkcode because it features improved interactivity and the ability to store identified issues.

checkcode(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 checkcode messages is subject to some change between versions.

checkcode(filename1,...,filenameN) displays messages for each specified filename.

example

checkcode(___,option1,...,optionN) 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.

example

info = checkcode(___,'-struct') returns the information as an n-by-1 structure array, where n is the number of messages found.

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] = checkcode(___) also returns filepaths, the absolute paths to the file names. You can specify filepaths with either the '-struct' or '-string' options.

Examples

collapse all

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 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).

Run checkcode on the example file lengthofline.m. Include message IDs and store the results in a structure.

info = checkcode('lengthofline', '-id')
info=13×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]

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 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 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 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.

  1. On the Home tab, in the Environment section, click the Preferences button.

  2. Select Code Analyzer in the left pane.

  3. Under Default Settings, in the Aesthetics and Readability section, clear the message Use instead of | as the OR operator in (scalar) conditional statements.

  4. Enter mysettings.txt as the file name and save it to your current folder.

  5. 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 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

collapse all

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

Display option, specified as one of these values. Options can appear in any order.

OptionDescription
'-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 the checkcode 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 individual case within a switch statement as 1, while modified cyclomatic complexity counts the entire switch 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

collapse all

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.

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 ==========

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

Version History

Introduced in R2011b