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 CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by