Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

checkcode

Check MATLAB code files for possible problems

Syntax

  • checkcode(filename)
    example
  • checkcode(filename1,...,filenameN)
  • checkcode(___,option1,...,optionN)
    example
  • info = checkcode(___,'-struct')
    example
  • msg = checkcode(___,'-string')
  • [___, filepaths] = checkcode(___)

Description

example

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 '-id' to request the message ID 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): 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.

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

Input Arguments

collapse all

File 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 {'lengthofline', 'buggy'}, 'collatz' as an input.

Example: 'lengthofline'

Example: {'lengthofline', 'buggy'}

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

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

More About

collapse all

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

See Also

|

Introduced in R2011b

Was this topic helpful?