Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
我继承了一些代码,使作者厌恶分号。是否可以一次性修复所有mlint消息(至少是所有具有自动修复的消息),而不必单击每个消息并按ALT + ENTER?
回答: 注意:此答案使用的函数MLINT ,在较新版本的MATLAB中不再推荐使用。首选较新的功能CHECKCODE ,并且下面的代码仍然可以通过简单地将对MLINT的调用替换为对该较新函数的调用来工作。 我不知道在一般的基于自动修复代码的方式MLINT消息。但是,在您的特定情况下,您可以采用一种自动方式将分号添加到发出MLINT警告的行中。 首先,让我们从此示例脚本junk.m : a = 1 b = 2; c = 'a' d = [1 2 3] e = 'hello'; 第一,第三和第四行将为您提供MLINT警告消息“ Terminate语句,用分号禁止输出(在脚本内)”。使用MLINT的功能形式,我们可以在文件中找到发生此警告的行。然后,我们可以从文件中读取所有代码行,在出现警告的行末添加分号,然后将代码行写回到文件中。这是这样做的代码: %# 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应该在每行末尾都包含分号。如果需要,可以将上面的代码放在一个函数中,以便可以在继承的代码的每个文件上轻松运行它。 更多&回答... |
![]() |
![]() |