checkcode
检查 MATLAB 代码文件是否有问题
语法
说明
注意
在 R2022b 中: 与 checkcode 相比,推荐使用 codeIssues,因为它具有改进的交互性和存储已识别问题的能力。
codeIssues 不测量圈复杂度。要测量圈复杂度,请使用 checkcode 以及 -cyc 或 -modcyc 选项。
checkcode( 显示有关 filename)filename 的消息,报告潜在的问题和代码改进机会。这些消息有时称为代码分析器消息。消息中的行号是一个超链接,点击后可以直接转到编辑器中的该行。视具体版本的不同,checkcode 消息的实际文字也会有所不同。
示例
对示例文件 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 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).
对示例文件 lengthofline.m 运行 checkcode。包含消息 ID,并将结果存储在结构体中。
info = checkcode('lengthofline', '-id')
info=16×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 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).
通过创建并指定设置文件,隐藏特定消息。例如,文件 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 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).
创建一个设置文件,用于隐藏标记使用 | 作为 OR 运算符的消息。
在主页选项卡的环境部分中,点击预设按钮。
从左窗格中选择代码分析器。
在默认设置下的美观性和可读性部分,清除消息请在(标量)条件语句中使用 而不是 | 作为 OR 运算符。
输入
mysettings.txt作为文件名,并将其保存到您的当前文件夹中。按取消按钮退出预设项面板,而不更改当前设置。
使用自定义设置文件 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 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).
输入参数
文件名,指定为字符向量、字符串数组或字符向量元胞数组。文件名可以包括部分路径,但必须位于搜索路径上的文件夹中或当前文件夹中。
如果 filename 是非标量字符串数组或字符向量元胞数组,则 MATLAB® 显示每个文件的信息。
注意
您不能合并文件名的元胞数组和字符数组。例如,不能将 {'lengthofline', 'buggy'}, 'collatz' 作为输入。
示例: 'lengthofline'
示例: {'lengthofline', 'buggy'}
数据类型: char | string
显示选项,指定为下列值之一。选项可以按任意顺序显示。
| 选项 | 描述 |
|---|---|
'-id' | 请求消息 ID,其中 ID 是字符向量。返回至结构体时,输出中还包含 id 字段,即与消息关联的 ID。 |
'-fullpath' | 假设输入文件名是绝对路径,因此 checkcode 不会尝试对其进行定位。 |
'-notok' | 对 有关 |
'-cyc' | 显示文件中每个函数的 McCabe 圈复杂度。一般来说,复杂度值越低表示程序越容易理解和修改。证据表明,程序越复杂,出错的可能性越大。通常可以通过将函数分解为更小、更简单的函数来降低复杂度。有人主张程序复杂度值超过 10 就应该分解。 有关圈复杂度的详细信息,请参阅Measure Code Complexity Using Cyclomatic Complexity。 |
'-modcyc' | 显示文件中每个函数的修正圈复杂度。除一处不同外,函数的修正圈复杂度等于 McCabe 圈复杂度。McCabe 圈复杂度将 有关圈复杂度的详细信息,请参阅Measure Code Complexity Using Cyclomatic Complexity。 |
| 用指定的设置文件覆盖默认的当前设置文件。如果指定的文件不在当前文件夹中,请提供该文件的完整路径。 有关创建设置文件的信息,请参阅保存并重新使用代码分析器消息设置。如果您指定的文件无效, 要忽略所有设置文件并使用出厂默认设置,请指定 |
输出参量
消息型信息,以 n×1 结构体数组形式返回,其中 n 是 checkcode 命令返回的消息数量。如果将多个文件名指定为输入,或将一个元胞数组指定为输入,info 会包含一个结构体元胞数组。
字段 | 描述 |
|---|---|
| 描述代码分析捕获的可疑结构的消息。 |
| 行号向量,指明消息适用于文件的哪些行。 |
| 一个包含列号(或列范围)的两列数组,指明消息适用于文件的哪些列。数组的第一列指定编辑器中消息的起始列。数组的第二列指定编辑器中消息的结束列。在这个两列数组中,每一条消息占一行。 |
消息型信息,以字符向量形式返回。如果将多个文件名指定为输入,或将一个元胞数组指定为输入,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
扩展功能
此函数完全支持基于线程的环境。有关详细信息,请参阅在基于线程的环境中运行 MATLAB 函数。
版本历史记录
在 R2011b 中推出
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)