matlab判断文件是否损坏,检查 MATLAB 代码文件是否有问题

示例

以下示例使用了 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值