Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

checkcode

检查 MATLAB 代码文件是否有问题

说明

示例

注意

在 R2022b 中: 与 checkcode 相比,推荐使用 codeIssues,因为它具有改进的交互性和存储已识别问题的能力。

checkcode(filename) 显示有关 filename 的消息,报告潜在的问题和代码改进机会。这些消息有时称为代码分析器消息。消息中的行号是一个超链接,点击后可以直接转到编辑器中的该行。视具体版本的不同,checkcode 消息的实际文字也会有所不同。

checkcode(filename1,...,filenameN) 显示每个指定 filename 的消息。

示例

checkcode(___,option1,...,optionN) 基于指定的选项标志修改返回的消息。例如,指定 '-modcyc' 以请求随每条消息返回修正圈复杂度。您可以使用上述语法中的任何输入参数指定选项。

示例

info = checkcode(___,'-struct')n×1 结构体数组形式返回信息,其中 n 是找到的消息数量。

msg = checkcode(___,'-string') 以字符向量形式返回信息。

如果省略 '-struct''-string' 参数并指定输出参数,则默认行为是 '-struct'

[___, filepaths] = checkcode(___) 还返回 filepaths,即文件名的绝对路径。您可以使用 '-struct''-string' 选项指定 filepaths

示例

全部折叠

对示例文件 lengthofline.m 运行 checkcode。MATLAB® 在命令行窗口中显示 lengthofline.m 的代码分析器消息。

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

对示例文件 lengthofline.m 运行 checkcode。包含消息 ID,并将结果存储在结构体中。

info = checkcode('lengthofline', '-id')
info=13×1 struct array with fields:
    id
    message
    fix
    line
    column

查看第一个消息的值。

info(1)
ans = struct with fields:
         id: 'NASGU'
    message: 'Value assigned to variable might be unused.'
        fix: 0
       line: 21
     column: [1 9]

使用 '-modcyc' 选项对示例文件 lengthofline.m 运行 checkcode。MATLAB® 会显示文件的修正圈复杂度,后跟 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).

通过创建并指定设置文件,隐藏特定消息。例如,文件 lengthofline.m 中有多个行使用 | 而不是 || 作为 OR 运算符。默认情况下,checkcode 会标记这些行。

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

创建一个设置文件,用于隐藏标记使用 | 作为 OR 运算符的消息。

  1. 主页选项卡的环境部分中,点击预设按钮。

  2. 从左窗格中选择代码分析器

  3. 默认设置下的美观性和可读性部分,清除消息请在(标量)条件语句中使用 而不是 | 作为 OR 运算符

  4. 输入 mysettings.txt 作为文件名,并将其保存到您的当前文件夹中。

  5. 取消按钮退出预设项面板,而不更改当前设置。

使用自定义设置文件 checkcode 对示例文件运行 mysettings.txt。消息请在(标量)条件语句中使用 而不是 | 作为 OR 运算符被隐藏,不再显示在消息列表中。

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

输入参数

全部折叠

文件名,指定为字符向量、字符串数组或字符向量元胞数组。文件名可以包括部分路径,但必须位于搜索路径上的文件夹中或当前文件夹中。

如果 filename 是非标量字符串数组或字符向量元胞数组,则 MATLAB® 显示每个文件的信息。

注意

您不能合并文件名的元胞数组和字符数组。例如,不能将 {'lengthofline', 'buggy'}, 'collatz' 作为输入。

示例: 'lengthofline'

示例: {'lengthofline', 'buggy'}

数据类型: char | string

显示选项,指定为下列值之一。选项可以按任意顺序显示。

选项描述
'-id'请求消息 ID,其中 ID 是字符向量。返回至结构体时,输出中还包含 id 字段,即与消息关联的 ID。
'-fullpath' 假设输入文件名是绝对路径,因此 checkcode 不会尝试对其进行定位。
'-notok'

filename 中的所有行运行 checkcode,包括那些以 checkcode 禁用指令 %#ok 结尾的行。

有关 %#ok 以及从程序中禁用消息的信息,请参阅调整代码分析器消息指示标记和消息

'-cyc'

显示文件中每个函数的 McCabe 圈复杂度。一般来说,复杂度值越低表示程序越容易理解和修改。证据表明,程序越复杂,出错的可能性越大。通常可以通过将函数分解为更小、更简单的函数来降低复杂度。有人主张程序复杂度值超过 10 就应该分解。

有关圈复杂度的详细信息,请参阅Measure Code Complexity Using Cyclomatic Complexity

'-modcyc'

显示文件中每个函数的修正圈复杂度。除一处不同外,函数的修正圈复杂度等于 McCabe 圈复杂度。McCabe 圈复杂度将 switch 语句中的每个单独 case 计数为 1,而修正圈复杂度将整个 switch 语句计数为 1。一般情况下,switch 语句比嵌套的 if-elseif-else 语句简单,因此,通常认为修正圈复杂度可以更好地衡量代码复杂度。

有关圈复杂度的详细信息,请参阅Measure Code Complexity Using Cyclomatic Complexity

'-config=settingsfile'

'-config=factory'

用指定的设置文件覆盖默认的当前设置文件。如果指定的文件不在当前文件夹中,请提供该文件的完整路径。

有关创建设置文件的信息,请参阅保存并重新使用代码分析器消息设置。如果您指定的文件无效,checkcode 会返回一个消息,指示不能打开或读取指定文件。这种情况下,checkcode 会使用出厂默认设置。

要忽略所有设置文件并使用出厂默认预设设置,请指定 '-config=factory'

输出参数

全部折叠

消息型信息,以 n×1 结构体数组形式返回,其中 ncheckcode 命令返回的消息数量。如果将多个文件名指定为输入,或将一个元胞数组指定为输入,info 会包含一个结构体元胞数组。

字段

描述

message

描述代码分析捕获的可疑结构的消息。

line

行号向量,指明消息适用于文件的哪些行。

column

一个包含列号(或列范围)的两列数组,指明消息适用于文件的哪些列。数组的第一列指定编辑器中消息的起始列。数组的第二列指定编辑器中消息的结束列。在这个两列数组中,每一条消息占一行。

消息型信息,以字符向量形式返回。如果将多个文件名指定为输入,或将一个元胞数组指定为输入,msg 会包含一个字符向量,其中每个文件的信息被分隔行分隔开,分隔行由 10 个等号字符、一个空格、文件名、一个空格和 10 个等号字符组成。

示例: ========== C:\MyMatlabFiles\buggy.m ==========

文件的绝对路径,指定为字符向量元胞数组。MATLAB 列出 filepaths 的顺序与指定的输入文件的顺序相同。

提示

要强制代码分析器忽略某一行代码,请在该行末尾使用 %#ok。可在该标记后添加注释。

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

扩展功能

版本历史记录

在 R2011b 中推出