Extracting the comments and the line of comments from a matlab script

10 ビュー (過去 30 日間)
Zahra Sheikhbahaee
Zahra Sheikhbahaee 2021 年 5 月 18 日
編集済み: Zahra Sheikhbahaee 2021 年 5 月 19 日
Is there anyway to extract the comments and the line of comments from a matlab script and save it in a text file?
  5 件のコメント
Steven Lord
Steven Lord 2021 年 5 月 19 日
Can you share a little more information about why you're trying to find and extract all the comments from a MATLAB program file? As others have stated this isn't going to be easy, and they were only considering files with the .m extension. The question of what constitutes a comment in a Live Script with the .mlx extension is more complicated -- does anything that's in a text section count as a comment (since it's not going to be executed) or only comments inside a code section?
Zahra Sheikhbahaee
Zahra Sheikhbahaee 2021 年 5 月 19 日
編集済み: Zahra Sheikhbahaee 2021 年 5 月 19 日
@Steven LordI have a vey long well documented code from other people and I need to make some changes to make it useful for my application. Being able to extract comments with the line of comments give me a summary of the code and where I should do my modifications because I would like to get a sense of algorithm like inner loop and outer loops to update some local and global parameters. That is my main reason.

サインインしてコメントする。

回答 (1 件)

DGM
DGM 2021 年 5 月 18 日
編集済み: DGM 2021 年 5 月 18 日
Normally, I'd just use sed
sed -n -e 's/^[[:blank:]]*//' -e '/^%/p' myscriptfile.m > pileofcomments.txt
I guess you could also use it in a system() call. I don't know if there's anything similar in Windows.
If you're looking for a strictly Matlab way, have fun. I have no familiarity with doing this in Matlab, so the solution I came up with is hardly elegant. Even so, I doubt the most elegant way of doing this can come close to the sed example.
fid = fopen('myscriptfile.m','r');
% read the file into a cell array, one cell per line
i = 1;
tline = fgetl(fid);
A{i} = tline;
while ischar(tline)
i = i+1;
tline = fgetl(fid);
A{i} = tline;
end
fclose(fid);
A = A.';
% get rid of empty lines
mk = cellfun(@ischar,A) & ~cellfun(@isempty,A);
A = A(mk);
% find matching lines
B = regexp(A,'^\s*%.*','match');
B = vertcat(B{:})
% optionally, get rid of leading whitespace
[~,C] = regexp(B,'^\s+','match','split')
for m = 1:numel(C)
C{m} = C{m}{end};
end
C % these are all the final lines
% write the comments to a new file
fid = fopen('pileofcomments.txt','w+');
for m = 1:numel(C)
% you may have to set the appropriate newline/cr characters
fprintf(fid,'%s\n',C{m});
end
fclose(fid);
EDIT:
It's worth noting that both of these solutions target lines which begin with comments (ignoring leading whitespace). They will not find comments at the end of lines like so:
% it will find this comment
% it will also find this comment
x = 2; % it will not find this comment
  4 件のコメント
DGM
DGM 2021 年 5 月 18 日
I second Walter's recommendation (I was looking on the FEX for it).
Adding that one extra requirement would greatly complicate things.
Jan
Jan 2021 年 5 月 19 日
編集済み: Jan 2021 年 5 月 19 日
See my comment above : The % masked in strings and CHAR vectors are a problem. Comments are started by "..." also. Block comments need to be considered also: %{ ... %}
@DGM: An easier method to read a text as a cell string:
S = fileread(FileName);
C = strsplit(S, '\n');
Removing leading and trailing blanks:
C = strtrim(C);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by