File Exchange

image thumbnail

% MATLAB Comment Stripping Toolbox

version (5.32 KB) by Peter John Acklam
A small collection of utilities for stripping MATLAB comments from strings and files.


Updated 22 Mar 2004

View License

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.

Cite As

Peter John Acklam (2021). % MATLAB Comment Stripping Toolbox (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (14)


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.

Alan Bindemann

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'

Timo Nicolai

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.

Al-Ismael Kiram

how to use this?


Very effective.

Rody Oldenhuis

Awesome, thanks :)

Oleg Komarov

Ideal for matlab contests...

Oh, besides of the NOT ignored lines after "...", also grouped comments are NOT ignored:

Great functions. Only my comments after a three points were left!

Muhammad Arsalan

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.

Scott Otterson

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.

Balaji Rangaswamy

Brett Shoelson

Excellent! This is regular expressions on steroids...makes quick work of comment stripping. Thanks, Peter.

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!