![]() |
有没有办法一次修复所有MATLAB mlint消息?
我继承了一些代码,使作者厌恶分号。是否可以一次性修复所有mlint消息(至少是所有具有自动修复的消息),而不必单击每个消息并按ALT + ENTER?
[B]回答:[/B] [B][I]注意:[/I][/B]此答案使用的函数[URL="http://www.mathworks.com/help/techdoc/ref/mlint.html"]MLINT[/URL] ,在较新版本的MATLAB中不再推荐使用。首选较新的功能[URL="http://www.mathworks.com/help/techdoc/ref/checkcode.html"]CHECKCODE[/URL] ,并且下面的代码仍然可以通过简单地将对MLINT的调用替换为对该较新函数的调用来工作。 我不知道[I]在一般的[/I]基于自动修复代码的方式[URL="http://www.mathworks.com/help/techdoc/ref/mlint.html"]MLINT[/URL]消息。但是,在您的[I]特定[/I]情况下,您可以采用一种自动方式将分号添加到发出[URL="http://www.mathworks.com/help/techdoc/ref/mlint.html"]MLINT[/URL]警告的行中。 首先,让我们从此示例脚本junk.m : a = 1 b = 2; c = 'a' d = [1 2 3] e = 'hello'; 第一,第三和第四行将为您提供[URL="http://www.mathworks.com/help/techdoc/ref/mlint.html"]MLINT[/URL]警告消息“ Terminate语句,用分号禁止输出(在脚本内)”。使用[URL="http://www.mathworks.com/help/techdoc/ref/mlint.html"]MLINT[/URL]的功能形式,我们可以在文件中找到发生此警告的行。然后,我们可以从文件中读取所有代码行,在出现警告的行末添加分号,然后将代码行写回到文件中。这是这样做的代码: %# Find the lines where a given mlint warning occurs: fileName = 'junk.m'; mlintID = 'NOPTS'; %# The ID of the warning mlintData = mlint(fileName,'-id'); %# Run mlint on the file index = strcmp({mlintData.id},mlintID); %# Find occurrences of the warnings... lineNumbers = [mlintData(index).line]; %# ... and their line numbers %# Read the lines of code from the file: fid = fopen(fileName,'rt'); linesOfCode = textscan(fid,'%s','Delimiter',char(10)); %# Read each line fclose(fid); %# Modify the lines of code: linesOfCode = linesOfCode{1}; %# Remove the outer cell array encapsulation linesOfCode(lineNumbers) = strcat(linesOfCode(lineNumbers),';'); %# Add ';' %# Write the lines of code back to the file: fid = fopen(fileName,'wt'); fprintf(fid,'%s\n',linesOfCode{1:end-1}); %# Write all but the last line fprintf(fid,'%s',linesOfCode{end}); %# Write the last line fclose(fid); 现在,文件junk.m应该在每行末尾都包含分号。如果需要,可以将上面的代码放在一个函数中,以便可以在继承的代码的每个文件上轻松运行它。 [url=https://stackoverflow.com/questions/3248295]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 01:15。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.