mlint
检查 MATLAB 代码文件是否有问题
兼容性
注意
不推荐使用 mlint
。请改用 checkcode
。
备选方法
有关如何使用代码分析器图形用户界面的信息,请参阅 使用代码分析器检查代码中的错误和警告。
语法
mlint('filename')
mlint('filename','-config=settings.txt')
mlint('filename','-config=factory')
inform=mlint('filename','-struct')
msg=mlint('filename','-string')
[inform,filepaths]=mlint('filename')
inform=mlint('filename','-id')
inform=mlint('filename','-fullpath')
inform=mlint('filename','-notok')
mlint('filename','-cyc')
mlint('filename','-codegen')
mlint('filename','-eml')
描述
mlint('filename')
显示有关 filename
的消息,报告潜在的问题和代码改进机会。这些消息有时称为代码分析器消息。消息中的行号是一个超链接,点击后可以直接转到编辑器中的该行。视具体版本的不同,mlint
消息的实际文字也会有所不同。
将 filename
指定为一个或多个字符向量或字符串数组,或者指定为字符向量元胞数组。如果 filename
指定多个字符向量或字符串数组,或者 filename
是非标量字符串数组或字符向量元胞数组,则 MATLAB® 显示每个文件的信息。您不能合并文件名的字符向量元胞数组和字符向量。例如,不能将 {'lengthofline', 'buggy'}, 'collatz'
作为输入。
mlint('filename','-config=settings.txt')
使用启用或禁用消息的设置覆盖默认当前设置文件,如指定的 settings.txt
文件中所指示。
注意
使用该参数时,对于 -config
选项所指定的 settings.txt
文件,必须指定其完整路径。
有关创建 settings.txt
文件的信息,请参阅保存并重新使用代码分析器消息设置。如果您指定的文件无效,mlint
会返回一个消息,指示不能打开或读取指定文件。这种情况下,mlint
会使用出厂默认设置。
mlint('filename','-config=factory')
忽略所有设置文件并使用出厂默认预设值。
inform=mlint('filename','-struct')
以结构体数组返回信息,数组的长度为找到消息的数目。该结构含有以下字段。
字段 | 描述 |
---|---|
| 描述代码分析捕获的可疑结构的消息。 |
| 消息所指的文件行号向量。 |
| 消息适用的文件列(或列范围),其形式为一个两列数组。数组的第一列指定编辑器中消息的起始列。数组的第二列指定编辑器中消息的结束列。两列数组的每一行表示一个消息。 |
如果指定多个文件名作为输入,则 inform
包含结构体的元胞数组。
msg=mlint('filename','-string')
以字符向量 msg
的形式返回信息。如果将多个文件名指定为输入,msg
会包含每个文件的信息,这些信息被分隔行分隔开,分隔行由 10 个等号字符 (=
)、一个空格、文件名、一个空格和 10 个等号字符组成。
如果省略 -struct
或 -string
参数并指定输出参数,则默认行为是 -struct
。如果省略参数且没有输出参数,默认行为是将信息显示到命令行。
[inform,filepaths]=mlint('filename')
还会根据文件名,按照指定的顺序返回所指定文件的绝对路径 filepaths
。
inform=mlint('filename','-id')
请求消息 ID,其中 ID 是 ABC...
形式的字符向量。返回至结构体时,输出中还包含 id
字段,即与消息关联的 ID。
inform=mlint('filename','-fullpath')
假设输入文件名是绝对路径,因此 mlint
不会尝试对其进行定位。
inform=mlint('filename','-notok')
对 filename
中的所有行运行 mlint
,包括那些以 mlint
禁用指令 %#ok
结尾的行也是如此。
mlint('filename','-cyc')
显示文件中每个函数的 McCabe 复杂度(也称为圈复杂度)。McCabe 复杂度值越大,表示复杂度越高,有证据表明,复杂度越高的值越可能包含错误。通常可以通过将函数分解为更小、更简单的函数来降低复杂度。一般来说,复杂度值越小表示程序更容易理解和修改。有人主张程序复杂度等级超过 10 就应该分解。
mlint('filename','-codegen')
可使代码生成消息显示在命令行窗口中。
不推荐使用 mlint('filename','-eml')
'-eml'
。请改用 '-codegen'
。
示例
以下示例使用了 lengthofline.m
,它是带有可改进代码的 MATLAB 示例文件。您可以在 matlabroot
/help/techdoc/matlab_env/examples
中可以找到该文件。如果要运行本例,请将 lengthofline.m
副本保存到 MATLAB 路径下。
不带任何选项的情况下对文件运行 mlint
要对示例文件 lengthofline.m
运行 mlint
,请运行以下指令:
mlint('lengthofline')
MATLAB 在命令行窗口中显示 lengthofline.m
的 M-Lint 消息:
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.
有关这些消息及如何改善这些代码的详细信息,请参阅 MATLAB 桌面工具和开发环境文档中的根据代码分析器消息更改代码。
以“显示 ID”和“将结果返回到结构体”选项运行 mlint
要将结果存储到结构体并包含消息 ID,请运行以下指令:
inform=mlint('lengthofline', '-id')
MATLAB 返回
inform = 19x1 struct array with fields: message line column id
要查看第一个消息的值,请运行以下指令:
inform(1)
MATLAB 显示
ans = message: 'The value assigned here to variable 'nothandle' might never be used.' line: 22 column: [1 9] id: 'NASGU'
此处,该消息表示,第 22 行上显示的值从文件中的第 1 列延伸至第 9 列。NASGU
是消息 'The value assigned here to variable 'nothandle' might never be used.'
的 ID。
使用 mlint 显示 McCabe 复杂度
要显示 MATLAB 代码文件的 McCabe 复杂度,需运行 mlint
并选择 -cyc
选项,如下例所示(假设已将 lengthofline.m
保存到本地文件夹)。
mlint lengthofline.m -cyc
命令行窗口中的结果显示了文件的 McCabe 复杂度,接着是 M-Lint 消息,如下所示:
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.
操作指南
关于禁用指令
%#ok
及程序中禁用消息的信息,请参阅调整代码分析器消息指示标记和消息。
扩展功能
版本历史记录
在 R2006a 之前推出