Is there any method to delete all the comments from a m-file.
I have a big file with 17,000 lines and i want to delete all comments from it.
Some comments are following the statements.
so plz HELP......!!!!!!!!!!!
> Is there any method to delete all the comments from a m-file.
> I have a big file with 17,000 lines and i want to delete all comments from it.
> Some comments are following the statements.
Look in the file exchange:
http://www.mathworks.com/matlabcentral/fileexchange/4645-matlab-comment-stripping-toolbox
http://www.mathworks.com/matlabcentral/fileexchange/7946-m-file-code-formatting-tool
Good luck, Jan
Sure. Something like
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fin = fopen(filename);
fout = fopen(tmpfilename,'tw');
while ~feof(fid)
s = fgetl(fid);
idx = min(find(s=='%'));
if ~isempty(idx)
if (idx > 2)
s = 1:idx-1;
else
s = [];
end
end
if ~isempty(s)
fwrite(fout,[s,'\n']);
end
end
fclose(fin);
fclose(fout);
delete(filename);
copyfile(tempfilename,filename);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
should get you close.
Rune
Be careful to not remove % in code such as formats for read/write
statements.
--
Loren
http://blogs.mathworks.com/loren
Are they so bad ;-)
Note the in most situations comments actually have a purpose. Are you sure you're better of in understanding and/or maintaining the software after ripping out the comments.
Jos
The below should get quite a bit closer than the first
attempt:
>> s = 'sscanf(''Test %d %%'',n) % Test string';
>> eatComments(s)
ans =
sscanf('Test %d %%',n)
Rune
%%%%%%%%%%%%%%%%%%%%%%%%%%% Start eatComments.m %%%%%%%%%%%%%%%%%%%%%%%
%function %function S = eatComments(s)
% S = eatComments - removes comments from the
% matlab source code string s.
% Basic idea: Check if the string contains
% a percent sign '%', and remove anything
% following the sign - including the sign.
%
% Complicating factors: percent signs might
% be part of legal matlab code, as part of
% format strings.
%
% Approach: If string contains '%' but no
% apostrophe " ' ", go right ahead. If string
% contains both, analyze where the % sign occurs:
% If there is an odd number of apostrophes
% before the % sign, % is a format sign.
% Else, the % sign denotes the start of a comment.
if ~ischar(s)
error('Argument must be a string');
end
[M,N] = size(s);
if (M~=1)
error('Argument must contain one row.')
end
idx1 = find(s=='%');
if isempty(idx1)
S = s;
return;
end
idx2 = find(s=='''');
if (isempty(idx2)||... % No apostrophes found
min(idx1)<min(idx2)) % First apostrophe in comment
ip = min(idx1);
if ip == 1
S = [];
return;
end
S = s(1:ip-1);
return;
end
n= findCommentSign(s);
if ~isempty(n)
S = s(1:n-1);
else
S=s;
end
end
function n = findCommentSign(s)
N = length(s);
npr = 0;
nap = 0;
pidx = 0;
aidx = 0;
for n=1:N
if (s(n)=='%')
npr = npr+1;
pidx = n;
end
if (s(n)=='''')
nap = nap+1;
aidx = n;
end
if (pidx > aidx) && (npr > 0) && (nap > 0) && mod(nap,2) == 0
return;
end
end
if s(n)~='%'
n = [];
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%% End eatComments.m %%%%%%%%%%%%%%%%%%%%%%%%
> Loren wrote:
> Be careful to not remove % in code such as formats for read/write
> statements.
To be exact: Ignore all % inside quoted strings.
This produces the next problem: How to find quoted strings? The subfunction ISQUOTED in SYMVAR is slow and does not recognize 2 subsequent quotes in a string, e.g. in:
Str = 'This string contains a quote: ''%this is no comment'
As summary the problem is quite complex and it's really worth to trust the functions from the file exchange.
Kind regards, Jan
Can I trust the FEX tools to work properly on this piece of code:
A=[1 2]'; B=[3 4]';
f = A'' * (double('''%') - 'A') * B''; %#function double ''' %#ok '' % <- remove HERE
% Bruno
a=pi;
%{
This is a comment in separate line, and it must be removed
%}
disp(a)
% Bruno
I know, the message is: Don't comment the code!
Rune
> Can I trust the FEX tools to work properly on this piece of code:
>
> A=[1 2]'; B=[3 4]';
> f = A'' * (double('''%') - 'A') * B''; %#function double ''' %#ok '' % <- remove HERE
>
> % Bruno
Follow me: Never trust a file from the FEX.
If a function is delivered with a unit-test and the function passes the unit-test, trust that the function passes exactly the unit-test and nothing else.
If the unit-test contains the a check of:
f = A'' * (double('''%') - 'A') * B''; %#function double ''' %#ok '' % <- remove HERE
and the test is correct, then one can trust that exactly blah blah blah.
What about:
x = [1, 2, ... ' % Valid comment in Matlab 6.5
3];
The best comment removing is printing with syntax coloring and scanning the printout with a green filter.
I was surprised that SYMVAR is weak. support@mathworks was not interested in my ideas for improving the recognition of quoted strings. The same error since (at least) Matlab 5.3, what a pitty.
No comment, Jan