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

Is there anyway to extract the comments and the line of comments from a matlab script and save it in a text file?

5 Comments

You can open the entire script as a text file, and then parse it with something like regexp.
@Jan has functions that parse scripts.
Finding comments is not easy, because of continuation markers and quoted strings and block comments.
@Walter Roberson: After I've inserted block comments in my parser, the modern double quotes for strings are a tedious new job.
a = "'%no comment'" % comment
b = '"%no comment"' % comment
c = '"'... comment " % comment
d = "asd"' % comment '
e = '''%no comment'
f = e' %comment
The serious problem is not finding the start of the comment, but to recognize strings and char vectors.
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?
@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.

Sign in to comment.

Answers (1)

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 Comments

Thanks for the answer! Well I was hoping I can get those comments too. Is there anyway to extract those cooments as well?
I second Walter's recommendation (I was looking on the FEX for it).
Adding that one extra requirement would greatly complicate things.
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);

Sign in to comment.

Categories

Asked:

on 18 May 2021

Edited:

on 19 May 2021

Community Treasure Hunt

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

Start Hunting!