查找MATLAB mlint警告ID的类别
我使用MATLAB中的checkcode函数在提供的文件名中为我提供所有错误消息的结构,以及它们的McCabe复杂性和与该错误相关的ID。i、 e查找MATLAB mlint警告ID的类别,matlab,debugging,Matlab,Debugging,我使用MATLAB中的checkcode函数在提供的文件名中为我提供所有错误消息的结构,以及它们的McCabe复杂性和与该错误相关的ID。i、 e info = checkcode(fileName, '-cyc','-id'); 在MATLAB的首选项中,有一个所有可能错误的列表,并将它们划分为不同的类别。如“美观易读”、“语法错误”、“不鼓励使用函数”等 有没有一种方法可以使用从上述代码行获得的错误ID访问这些类别?只是为了解决这个问题。我设法从几个不同的地方提取数据并将其拼凑在一起。我现
info = checkcode(fileName, '-cyc','-id');
在MATLAB的首选项中,有一个所有可能错误的列表,并将它们划分为不同的类别。如“美观易读”、“语法错误”、“不鼓励使用函数”等
有没有一种方法可以使用从上述代码行获得的错误ID访问这些类别?只是为了解决这个问题。我设法从几个不同的地方提取数据并将其拼凑在一起。我现在有一个excel电子表格,其中列出了所有matlab的警告和错误,以及相应的ID代码、类别和严重性(即,如果是警告或错误)。我现在可以读入这个文件,查找使用“checkcode”功能获得的ID代码,并提取所需的任何信息。现在可以使用它来创建分析工具,以查看编写的脚本/类等的质量 如果有人想要这个文件的副本,请给我留言,我很乐意提供
达伦。对于这个问题,我在脑海中反复思考了不同的想法,最终找到了一个最优雅的解决方案来解决这个问题 解决方案 此解决方案的关键组件是标志。如果提供此参数,则会打印
mlint
id、严重性代码和描述的完整列表。更重要的是,这些类别也打印在此列表中,所有mlint
id都列在各自的mlint
类别下面
行刑
现在我们不能简单地用-allmsg
标志调用checkcode
(或mlint
),因为这太容易了。相反,它需要一个实际的文件来尝试解析和检查错误。您可以传递任何有效的m文件,但我选择传递内置的sum.m
,因为实际的文件本身只包含帮助信息(因为它的实际实现很可能是C++),因此mlint
能够在没有警告的情况下非常快速地解析它
checkcode('sum.m', '-allmsg');
打印到命令窗口的输出摘录如下:
INTER ========== Internal Message Fragments ==========
MSHHH 7 this is used for %#ok and should never be seen!
BAIL 7 done with run due to error
INTRN ========== Serious Internal Errors and Assertions ==========
NOLHS 3 Left side of an assignment is empty.
TMMSG 3 More than 50,000 Code Analyzer messages were generated, leading to some being deleted.
MXASET 4 Expression is too complex for code analysis to complete.
LIN2L 3 A source file line is too long for Code Analyzer.
QUIT 4 Earlier syntax errors confused Code Analyzer (or a possible Code Analyzer bug).
FILER ========== File Errors ==========
NOSPC 4 File <FILE> is too large or complex to analyze.
MBIG 4 File <FILE> is too big for Code Analyzer to handle.
NOFIL 4 File <FILE> cannot be opened for reading.
MDOTM 4 Filename <FILE> must be a valid MATLAB code file.
BDFIL 4 Filename <FILE> is not formed from a valid MATLAB identifier.
RDERR 4 Unable to read file <FILE>.
MCDIR 2 Class name <name> and @directory name do not agree: <FILE>.
MCFIL 2 Class name <name> and file name do not agree: <file>.
CFERR 1 Cannot open or read the Code Analyzer settings from file <FILE>. Using default settings instead.
...
MCLL 1 MCC does not allow C++ files to be read directly using LOADLIBRARY.
MCWBF 1 MCC requires that the first argument of WEBFIGURE not come from FIGURE(n).
MCWFL 1 MCC requires that the first argument of WEBFIGURE not come from FIGURE(n) (line <line #>).
NITS ========== Aesthetics and Readability ==========
DSPS 1 DISP(SPRINTF(...)) can usually be replaced by FPRINTF(...).
SEPEX 0 For better readability, use newline, semicolon, or comma before this statement.
NBRAK 0 Use of brackets [] is unnecessary. Use parentheses to group, if needed.
...
如果你感兴趣的话,这里有一个小函数
function [warnings, categories] = mlintCatalog()
% Get a list of all categories, mlint IDs, and severity rankings
output = evalc('checkcode sum.m -allmsg');
% Break each line into it's components
lines = regexp(output, '\n', 'split').';
pattern = '^\s*(?<id>[^\s]*)\s*(?<severity>\d*)\s*(?<message>.*?\s*$)';
warnings = regexp(lines, pattern, 'names');
warnings = cat(1, warnings{:});
% Determine which ones are category names
isCategory = cellfun(@isempty, {warnings.severity});
categories = warnings(isCategory);
% Fix up the category names
pattern = '(^\s*=*\s*|\s*=*\s*$)';
messages = {categories.message};
categoryNames = cellfun(@(x)regexprep(x, pattern, ''), messages, 'uni', 0);
[categories.message] = categoryNames{:};
% Now pair each mlint ID with it's category
comp = bsxfun(@gt, 1:numel(warnings), find(isCategory).');
[category_id, ~] = find(diff(comp, [], 1) == -1);
category_id(end+1:numel(warnings)) = numel(categories);
% Assign a category field to each mlint ID
[warnings.category] = categoryNames{category_id};
category_id = num2cell(category_id);
[warnings.category_id] = category_id{:};
% Remove the categories from the warnings list
warnings = warnings(~isCategory);
% Convert warning severity to a number
severity = num2cell(str2double({warnings.severity}));
[warnings.severity] = severity{:};
% Save just the categories
categories = rmfield(categories, 'severity');
% Convert array of structs to a struct where the MLINT ID is the field
warnings = orderfields(cell2struct(num2cell(warnings), {warnings.id}));
end
函数[警告,类别]=mlintCatalog()
%获取所有类别、mlint ID和严重性排名的列表
输出=evalc('checkcode sum.m-allmsg');
%将每条线分解为各个部分
lines=regexp(输出“\n”、“拆分”);
模式=“^\s*(?[^\s]*)\s*(?\d*)\s*(?*?*\s*$)”;
warnings=regexp(行、模式、“名称”);
警告=类别(1,警告{:});
%确定哪些是类别名称
isCategory=cellfun(@isempty,{warnings.severity});
类别=警告(属类别);
%确定类别名称
模式='(^\s*=*\s*|\s*=*\s*$);
messages={categories.message};
categoryNames=cellfun(@(x)regexprep(x,pattern.)),消息,'uni',0);
[categories.message]=categoryNames{:};
%现在将每个mlint ID与其类别配对
comp=bsxfun(@gt,1:numel(警告),find(isCategory)。”;
[category_id,~]=find(diff(comp,[],1)=-1);
类别id(结束+1:numel(警告))=numel(类别);
%为每个mlint ID分配一个类别字段
[警告.类别]=类别名称{category_id};
类别标识=num2单元(类别标识);
[warnings.category_id]=category_id{:};
%从警告列表中删除类别
警告=警告(~isCategory);
%将警告严重性转换为数字
severity=num2cell(str2double({warnings.severity}));
[警告.严重性]=严重性{:};
%只保存类别
类别=rmfield(类别,“严重性”);
%将结构数组转换为MLINT ID为字段的结构
warnings=orderfields(cell2struct(num2cell(warnings),{warnings.id}));
结束
总结
这是一种完全没有文档记录但相当可靠的方法,可以获取与给定mlint
ID关联的类别和严重性。此功能存在于2010年,甚至在此之前,因此它应该适用于您必须处理的任何MATLAB版本。这种方法也比简单地记录给定的mlint
ID所属的类别灵活得多,因为随着新函数的添加和旧函数的弃用,类别(和严重性)将随着版本的变化而变化
谢谢你提出这个富有挑战性的问题,我希望这个答案能提供一些帮助和见解 你能把一些文档链接到解释这些类别的地方吗?我找不到它…在“主页”选项卡上,单击“环境”部分的“首选项”,然后选择左侧的“代码分析器”。所有类别都在那里。我只是想澄清一下,我的目的是在一些函数中输入一个错误ID,例如“DWVRD”(对应于错误“wavread将在将来的版本中删除,改用audioread”),这将返回此错误代码所列的类别以及与之相关的任何其他属性。我认为您只需硬编码即可…我意识到可能没有一个直接的解决方案/内置函数可以做到这一点。我在问如何避开它。如果“首选项”菜单中的错误按类别列出,则必须将此信息存储在某个位置。你知道我可以如何访问它吗?这似乎是一个比使用我创建的excel文件来存储它们更好的解决方案。我一定会用这个更新我的方法。谢谢。+1我对文本编辑器的linter插件也有类似的建议:
function [warnings, categories] = mlintCatalog()
% Get a list of all categories, mlint IDs, and severity rankings
output = evalc('checkcode sum.m -allmsg');
% Break each line into it's components
lines = regexp(output, '\n', 'split').';
pattern = '^\s*(?<id>[^\s]*)\s*(?<severity>\d*)\s*(?<message>.*?\s*$)';
warnings = regexp(lines, pattern, 'names');
warnings = cat(1, warnings{:});
% Determine which ones are category names
isCategory = cellfun(@isempty, {warnings.severity});
categories = warnings(isCategory);
% Fix up the category names
pattern = '(^\s*=*\s*|\s*=*\s*$)';
messages = {categories.message};
categoryNames = cellfun(@(x)regexprep(x, pattern, ''), messages, 'uni', 0);
[categories.message] = categoryNames{:};
% Now pair each mlint ID with it's category
comp = bsxfun(@gt, 1:numel(warnings), find(isCategory).');
[category_id, ~] = find(diff(comp, [], 1) == -1);
category_id(end+1:numel(warnings)) = numel(categories);
% Assign a category field to each mlint ID
[warnings.category] = categoryNames{category_id};
category_id = num2cell(category_id);
[warnings.category_id] = category_id{:};
% Remove the categories from the warnings list
warnings = warnings(~isCategory);
% Convert warning severity to a number
severity = num2cell(str2double({warnings.severity}));
[warnings.severity] = severity{:};
% Save just the categories
categories = rmfield(categories, 'severity');
% Convert array of structs to a struct where the MLINT ID is the field
warnings = orderfields(cell2struct(num2cell(warnings), {warnings.id}));
end