Quantitative finance collector
C++ Matlab VBA/Excel Java Mathematica R/Splus Net Code Site Other
May 22

Matlab Comment Stripping

Posted by abiao at 10:49 | Code » Matlab | Comments(0) | Reads(7876)
I have been doing an internship in London since last week and haven't got enough time to write new posts, sorry for that. So I plan to write a few posts to summarize the old Matlab functions I personally like a lot.  The one for today is Matlab comment stripping.
Open in new window

Needless to say, comment is crucial for programming, it helps ourselves to debug our thoughts and other colleagues to understand the codes efficiently, which is especially indispensable as each programmer has his own coding style. However, under certain circumstances we may have to remove those comments, for instance, for the sake of confidential, etc. How do you do that then? delete the comments line by line? it is OK for a small file, but generally we may end up with a file with dozens, if not hundreds, lines of  comments, what's worse is those comments intersect and we have to be carefully to find them out.

Here is a clever way, MATLAB Comment Stripping Toolbox (it indeed has only 3 short files if you are afraid by "Toolbox" grin). Basically it is a small collection of utilities for stripping MATLAB comments from MATLAB code. The code may be given in strings, cell arrays of strings, or read from a file or file identifier. There is full support for stripping comments from multi-line strings, that is strings with embedded newlines, which typically appear when an entire file is read in one go. For example, I wrote a simple test.m file and prefer to remove the comments and rename it as test-client.m:
% test the comment strip command using mlstripcommentsfile(infile, outfile)
% by abiao @ mathfinance.cn
a = rnorm(10,1); % check if you can remove me
% one more line
b = rnorm(5,5);
c = rnorm(3,3)...
    + rnorm(3,3); %now i am here
% test finished


after running the command mlstripcommentsfile('test.m', 'test-client.m'), I got the file test-client.m to send to others


a = rnorm(10,1);

b = rnorm(5,5);
c = rnorm(3,3)...
    + rnorm(3,3);



Nice, isn't it? you can download the toolbox at http://www.mathworks.com/matlabcentral/fileexchange/4645.
PS: the original codes use old Matlab version so you will get the warning "Warning: The 'tokenize' option for regexprep is now the default behavior." You may choose to modify the codes by deleting 'tokenize'.


Tags:
Add a comment
Emots
Enable HTML
Enable UBB
Enable Emots
Hidden
Remember
Nickname   Password   Optional
Site URI   Email   [Register]