Don't think so in on go...best I can think of is to read it with the first option and write that as a temporary file then process that file with the second.

I think textscan will only handle the case of '/*' '*/' pairs if they're first on the line, however, so perhaps you could get the same effect by reading the whole file as a char() array w/ fread, do a global substitution for strrep('/*','//') and then process that from memory?

Failing that, since one would presume c source files aren't terribly long, read line-by-line w/ fgetl and do the search on each line for either. This will let you get the end of line comments to as a side benefit to make up for the extra complexity...

ADDENDUM:

I reviewed the doc's for textscan; looks like it would catch the /* */ sequences correctly; what I recalled as beginning of line is actually written/documented as beginning of field.

The problem is not uniquely defined. How can you decide which comment style is used? What will happen for:

this is the contents /* and an intermediate // C++ comment */

Now removing the C++ comment at first will invalidate the file. The same happens, when % comments appear also. Another example:

this is contents // Here a C comment starts /*
this is contents // Here the C comment ends */

Does textscan consider quoted strings?

fprintf("This is not a comment: // %s\n", "hello");

By the way, if you mean Matlab comments, consider {% %} and ... also, because both start comments as % does.

Parsing comments is not trivial. The most reliable technique is to let Matlab's editor display the text with syntax highlighting, capture the screen, delete green pixels, use an OCR tool to parse the rest. ;-)

which seems to be working in standard situations. Of course, managing all situations including pathological ones would require more work, but this has the advantage to be simple and you might not need to manage pathological cases, especially if you wrote these C files yourself, consistently and with rigor.

@Cedric: Providing your test data is a rock solid definition of the capabilities of your code. While regexp expressions always look like somebody has rolled an angry armadillo over the keyboard, the test data are clear due to their simplicity. +1

Yes, my list was not complete. I'm sure it can be enlarged with many more entries! It's easier if you have a fixed-format source code like I usually have with Fortran 77. You just search for "C" in column 1 ...