Editor's Note: This file was selected as MATLAB Central Pick of the Week
This 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.
Peter John Acklam (2021). % MATLAB Comment Stripping Toolbox (https://www.mathworks.com/matlabcentral/fileexchange/4645-matlab-comment-stripping-toolbox), MATLAB Central File Exchange. Retrieved .
Inspired: removeComments(filename,commentIndicator) , MATLAB Repo Comment Stripper, missingsemicolons
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Create scripts with code, output, and formatted text in a single executable document.
The main regex can be easily extended to allow the string datatype: mainregex='((^|\n)(([\]\)}\w.]''+|[^''"%](?!\.\.))+|(''[^''\n]*(''''[^''\n]*)*'')|("[^"\n]*(""[^"\n]*)*"))*)[^\n]*';
This will not quite remove the line continuations correctly (and it ignores block comments), but it is not too difficult to extend this function to fix those two issues.
Love the concept and the code is very well written and documented, but, in addition to the multiline comments issue highlighted by others, users should be aware that the new strings feature of MATLAB also aren't handled.
for example 'A = "I am 100% sure this will work"'; is returns 'A = "I am 100'
Does not seem to work (Matlab R2016b), mlstripcommentsfile produces empty output files.
I will improve my rating once the missing feature of removing %{ ... %} multiline comments is added. Besides that it's fantastic.
how to use this?
Very effective.
Awesome, thanks :)
Ideal for matlab contests...
Oh, besides of the NOT ignored lines after "...", also grouped comments are NOT ignored:
%{
bla
bla
%}
Carlos
Great functions. Only my comments after a three points were left!
hi, very nice application. But when it works perfect up to line 277 after that it don't work as it supposed to work. There there anything that i can change?? like some memory limitations?? i need to use it on a file with 17000 lines. Any suggestions please.
Useful. A bit out of date, though: With R14, I'm getting a many copies of this warning:
Warning: The 'tokenize' option for regexprep is now the default behavior. If I remove 'tokenize' it goes away.
Excellent! This is regular expressions on steroids...makes quick work of comment stripping. Thanks, Peter.