11

著者がセミコロンを嫌うコードをいくつか継承しました。mlint メッセージを 1 つずつクリックして Alt + Enter キーを押すのではなく、一度に (少なくともすべてのメッセージを自動修正で) 修正することはできますか?

4

3 に答える 3

8

注:この回答では、関数MLINTを使用しています。これは、MATLABの新しいバージョンでは推奨されなくなりました。新しい関数CHECKCODEが推奨されます。以下のコードは、MLINTの呼び出しをこの新しい関数の呼び出しに置き換えるだけで機能します。


MLINTメッセージに基づいてコードを自動的に修正する一般的な 方法がわかりません。ただし、特定のケースでは、MLINT警告をスローする行にセミコロンを追加する自動化された方法があります。

まず、このサンプルスクリプトから始めましょうjunk.m

a = 1
b = 2;
c = 'a'
d = [1 2 3]
e = 'hello';

1行目、3行目、および4行目には、MLINT警告メッセージ「(スクリプト内で)出力を抑制するためにセミコロンでステートメントを終了してください」というメッセージが表示されます。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のすべての行の終わりにセミコロンが表示されます。必要に応じて、上記のコードを関数に入れて、継承されたコードのすべてのファイルで簡単に実行できるようにすることができます。

于 2010-07-14T17:37:40.273 に答える
6

これが古い投稿であることは知っていますが、最近これが必要になり、元のコードを少し改善したので、他の誰かがそれを必要とする場合はここにあります. 欠落している「;」を探します 通常のスクリプトだけでなく、関数内では、コード内の空白を維持し、何かが変更されたファイルのみを書き込みます。

function [] = add_semicolon(fileName)
%# Find the lines where a given mlint warning occurs:

mlintIDinScript = 'NOPTS';                       %# The ID of the warning
mlintIDinFunction = 'NOPRT';
mlintData = mlint(fileName,'-id');       %# Run mlint on the file
index = strcmp({mlintData.id},mlintIDinScript) | strcmp({mlintData.id},mlintIDinFunction);  %# Find occurrences of the warnings...
lineNumbers = [mlintData(index).line];   %#   ... and their line numbers

if isempty(lineNumbers)
    return;
end;
%# Read the lines of code from the file:

fid = fopen(fileName,'rt');
%linesOfCode = textscan(fid,'%s', 'Whitespace', '\n\r');  %# Read each line
lineNo = 0;
tline = fgetl(fid);
while ischar(tline)
    lineNo = lineNo + 1;
    linesOfCode{lineNo} = tline;
    tline = fgetl(fid);
end
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:

fim = fopen(fileName,'wt');
fprintf(fim,'%s\n',linesOfCode{1:end-1});  %# Write all but the last line
fprintf(fim,'%s',linesOfCode{end});        %# Write the last line
fclose(fim);
于 2012-03-08T13:18:17.987 に答える