Documentation Center

  • Trial Software
  • Product Updates

checkcode

Check MATLAB code files for possible problems

Alternatives

For information on using the graphical user interface for checking code, see Check Code for Errors and Warnings.

Syntax

checkcode('filename')
checkcode('filename','-config=settings.txt')
checkcode('filename','-config=factory')
inform=checkcode('filename','-struct')
msg=checkcode('filename','-string')
[inform,filepaths]=checkcode('filename')
inform=checkcode('filename','-id')
inform=checkcode('filename','-fullpath')
inform=checkcode('filename','-notok')
checkcode('filename','-cyc')
checkcode('filename','-codegen')
checkcode('filename','-eml')

Description

checkcode('filename') displays messages, sometimes referred to as Code Analyzer messages, about filename, where the message reports potential problems and opportunities for code improvement. The line number in the message is a hyperlink that opens the file in the Editor, scrolled to that line. If filename is a cell array, information is displayed for each file. For checkcode(F1,F2,F3,...), where each input is a character array, MATLAB® software displays information about each input file name. You cannot combine cell arrays and character arrays of file names. Note that the exact text of the checkcode messages is subject to some change between versions.

checkcode('filename','-config=settings.txt') overrides the default active settings file with the settings that enable or suppress messages as indicated in the specified settings.txt file.

    Note:   If used, you must specify the full path to the settings.txt file specified with the -config option.

For information about creating a settings.txt 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.

checkcode('filename','-config=factory') ignores all settings files and uses the factory default preference settings.

inform=checkcode('filename','-struct') returns the information in a structure array whose length is the number of messages found. The structure has the fields that follow.

Field

Description

message

Message describing the suspicious construct that code analysis caught.

line

Vector of file line numbers to which the message refers.

column

Two-column array of file columns (column extents) to which the message applies. 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. There is one row in the two-column array for each occurrence of a message.

If you specify multiple file names as input, or if you specify a cell array as input, inform contains a cell array of structures.

msg=checkcode('filename','-string') returns the information as a string to the variable msg. If you specify multiple file names as input, or if you specify a cell array as input, msg contains a string where each file's information is separated by 10 equal sign characters (=), a space, the file name, a space, and 10 equal sign characters.

If you omit the -struct or -string argument and you specify an output argument, the default behavior is -struct. If you omit the argument and there are no output arguments, the default behavior is to display the information to the command line.

[inform,filepaths]=checkcode('filename') additionally returns filepaths, the absolute paths to the file names, in the same order as you specified them.

inform=checkcode('filename','-id') requests the message ID, where ID is a string of the form ABC.... When returned to a structure, the output also has the id field, which is the ID associated with the message.

inform=checkcode('filename','-fullpath') assumes that the input file names are absolute paths, so that checkcode does not try to locate them.

inform=checkcode('filename','-notok') runs checkcode for all lines in filename, even those lines that end with the checkcode suppression directive, %#ok.

checkcode('filename','-cyc') displays the McCabe complexity (also referred to as cyclomatic complexity) of each function in the file. Higher McCabe complexity values indicate higher complexity, and there is some evidence to suggest 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. In general, smaller complexity values indicate programs that are easier to understand and modify. Some people advocate splitting up programs that have a complexity rating over 10.

checkcode('filename','-codegen') enables code generation messages for display in the Command Window.

checkcode('filename','-eml') '-eml' is not recommended. Use '-codegen' instead.

Examples

The following examples use lengthofline.m, which is a sample file with MATLAB code that can be improved. You can find it in matlabroot/help/techdoc/matlab_env/examples. If you want to run the examples, save a copy of lengthofline.m to a location on your MATLAB path.

Running checkcode on a File with No Options

To run checkcode on the example file, lengthofline.m, run

checkcode('lengthofline')

MATLAB displays the Code Analyzer messages for lengthofline.m in the Command Window:

L 22 (C 1-9): The value assigned here to variable 'nothandle' might never be used.
L 23 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).
L 24 (C 5-11): 'notline' might be growing inside a loop. Consider preallocating for speed.
L 24 (C 44-49): Use STRCMPI(str1,str2) instead of using LOWER in a call to STRCMP.
L 28 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).
L 34 (C 13-16): 'data' might be growing inside a loop. Consider preallocating for speed.
L 34 (C 24-31): Use dynamic fieldnames with structures instead of GETFIELD. Type 'doc struct' for more information.
L 38 (C 29): 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 40 (C 47): Use || instead of | as the OR operator in (scalar) conditional statements.
L 42 (C 13-16): 'data' might be growing inside a loop. Consider preallocating for speed.
L 43 (C 13-15): 'dim' might be growing inside a loop. Consider preallocating for speed.
L 45 (C 13-15): 'dim' might be growing inside a loop.Consider preallocating for speed.
L 48 (C 52): There may be a parenthesis imbalance around here.
L 48 (C 53): There may be a parenthesis imbalance around here.
L 48 (C 54): There may be a parenthesis imbalance around here.
L 48 (C 55): There may be a parenthesis imbalance around here.
L 49 (C 17): Terminate statement with semicolon to suppress output (in functions).
L 49 (C 23): Use of brackets [] is unnecessary. Use parentheses to group, if needed.

For details about these messages and how to improve the code, see Changing Code Based on Code Analyzer Messages in the MATLAB Desktop Tools and Development Environment documentation.

Running checkcode with Options to Show IDs and Return Results to a Structure

To store the results to a structure and include message IDs, run

inform=checkcode('lengthofline', '-id')

MATLAB returns

inform = 

19x1 struct array with fields:
    message
    line
    column
    id

To see values for the first message, run

inform(1)

MATLAB displays

ans = 

    message: 'The value assigned here to variable 'nothandle' might never be used.'
       line: 22
     column: [1 9]
         id: 'NASGU'

Here, the message is for the value that appears on line 22 that extends from column 1–9 in the file.NASGU is the ID for the message 'The value assigned here to variable 'nothandle' might never be used.'.

Displaying McCabe Complexity with checkcode

To display the McCabe complexity of a MATLAB code file, run checkcode with the -cyc option, as shown in the following example (assuming you have saved lengthofline.m to a local folder).

checkcode lengthofline.m -cyc

Results displayed in the Command Window show the McCabe complexity of the file, followed by the Code Analyzer messages, as shown here:

L 1 (C 23-34): The McCabe complexity of 'lengthofline' is 12.
L 22 (C 1-9): The value assigned here to variable 'nothandle' might never be used.
L 23 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).
L 24 (C 5-11): 'notline' might be growing inside a loop. Consider preallocating for speed.
L 24 (C 44-49): Use STRCMPI(str1,str2) instead of using UPPER/LOWER in a call to STRCMP.
L 28 (C 12-15): NUMEL(x) is usually faster than PROD(SIZE(x)).
L 34 (C 13-16): 'data' might be growing inside a loop. Consider preallocating for speed.
L 34 (C 24-31): Use dynamic fieldnames with structures instead of GETFIELD. Type 'doc struct' for more information.
L 38 (C 29): 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 40 (C 47): Use || instead of | as the OR operator in (scalar) conditional statements.
L 42 (C 13-16): 'data' might be growing inside a loop. Consider preallocating for speed.
L 43 (C 13-15): 'dim' might be growing inside a loop. Consider preallocating for speed.
L 45 (C 13-15): 'dim' might be growing inside a loop. Consider preallocating for speed.
L 48 (C 52): There may be a parenthesis imbalance around here.
L 48 (C 53): There may be a parenthesis imbalance around here.
L 48 (C 54): There may be a parenthesis imbalance around here.
L 48 (C 55): There may be a parenthesis imbalance around here.
L 49 (C 17): Terminate statement with semicolon to suppress output (in functions).
L 49 (C 23): Use of brackets [] is unnecessary.  Use parentheses to group, if needed.

See Also

mlintrpt, profile

How To

Was this topic helpful?