Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Deleting all comments present in a m-file

473 views
Skip to first unread message

Muhammad Arsalan

unread,
Oct 9, 2009, 5:29:03 AM10/9/09
to
Hi,

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......!!!!!!!!!!!

Jan Simon

unread,
Oct 9, 2009, 6:30:19 AM10/9/09
to
Dear Muhammad Arsalan!

> 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

Muhammad Arsalan

unread,
Oct 9, 2009, 6:46:02 AM10/9/09
to
Jan---------- 17,000 Thanks .....

Rune Allnor

unread,
Oct 9, 2009, 6:54:06 AM10/9/09
to
On 9 Okt, 11:29, "Muhammad Arsalan" <arsalanche...@gmail.com> wrote:
> Hi,
>
> 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.

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

Loren Shure

unread,
Oct 9, 2009, 7:14:46 AM10/9/09
to
In article <hamvov$9fc$1...@fred.mathworks.com>, arsala...@gmail.com
says...


Be careful to not remove % in code such as formats for read/write
statements.

--
Loren
http://blogs.mathworks.com/loren

Jos

unread,
Oct 9, 2009, 8:55:20 AM10/9/09
to
"Muhammad Arsalan" <arsala...@gmail.com> wrote in message <hamvov$9fc$1...@fred.mathworks.com>...

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

Rune Allnor

unread,
Oct 9, 2009, 10:31:13 AM10/9/09
to
On 9 Okt, 13:14, Loren Shure <loren.sh...@mathworks.com> wrote:
> In article <hamvov$9f...@fred.mathworks.com>, arsalanche...@gmail.com

> says...
>
> > Hi,
>
> > 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......!!!!!!!!!!!
>
> Be careful to not remove % in code such as formats for read/write
> statements.
>
> --
> Lorenhttp://blogs.mathworks.com/loren


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 %%%%%%%%%%%%%%%%%%%%%%%%

Jan Simon

unread,
Oct 9, 2009, 10:40:04 AM10/9/09
to
Dear Muhammad!

> 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

Bruno Luong

unread,
Oct 9, 2009, 11:04:02 AM10/9/09
to
"Jan Simon" <matlab.T...@nMINUSsimon.de> wrote in message <hani04$cm1$1...@fred.mathworks.com>...

> Dear Muhammad!
>
> > 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'

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

Bruno Luong

unread,
Oct 9, 2009, 11:17:20 AM10/9/09
to
Here is another example:

a=pi;
%{
This is a comment in separate line, and it must be removed
%}
disp(a)

% Bruno

Rune Allnor

unread,
Oct 9, 2009, 11:24:48 AM10/9/09
to

I know, the message is: Don't comment the code!

Rune

Jan Simon

unread,
Oct 9, 2009, 4:22:17 PM10/9/09
to
Dear Bruno!

> 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

Wasim Aftab

unread,
Jan 12, 2016, 7:53:15 PM1/12/16
to
"Muhammad Arsalan" <arsala...@gmail.com> wrote in message <hamvov$9fc$1...@fred.mathworks.com>...
Hi Muhammed,
Open the m file in notepad++. Then press Ctrl+F to pop up find window, in that window clik on Replace, after that select 'Regular expression' (at bottom of that window). Then paste this ^%+[\w\W]+ in the 'Find what' box and keep 'Replace with' box empty. Then click on Replace All.
That's it.
0 new messages