File Exchange

image thumbnail


version 1.0 (1.71 KB) by

count coded and commented lines in an m-file



View License

N = COUNTCODE(MFILE) returns the number of code lines in MFILE. MFILE should be a string containing a name to an existing m-file or asv-file.

[N,M] = COUNTCODE(MFILE) also returns the number M of commented lines in the file. [N,M,S,Q] = ... further returns a cell array of strings S containing all lines of the file (code and comment, but excluding empty lines) and a logical vector Q with ones for the comment lines of S.

Note: this file use <regexp>

Comments and Ratings (7)


Pavan (view profile)

Nice and useful function.

sri harsha boddepalli

Raymond Norris


It looks like your code is checking for the number of lines of text (sans comments), not the number of line of code. Specifically, if a line of code is continued to the next line with elipse (...), COUNTCODE will treat this as two separate lines of code.

Look at SLOC ( as an example that handles block comments and continued lines. I like your approach (10x faster than mine), you just need to do a little more parsing.

Jos x

An update (with which and total lines) is on its way. Unfortunately, I do not have R14 yet ... Poor me ...

John D'Errico

Not bad, but it needs to count block comments, and the requirement of needing a full path to a file is silly when which will give it that information.

Jiro Doke

Very nicely done. One request (personal preference) might be a capability to run this for any file in the path (instead of having to type in the full path). Maybe if the file is not found, then do a WHICH to see the file exists in another place.

Other than that, good documentation and error checking.

urs (us) schwarz

nice function with good help and sound engine
two minor drawbacks
- it would be nice to get the total number of lines of a file as well (akin to the unix WC)
- it doesn't know - yet - how to deal with block comments (a new r14+ feature)

MATLAB Release
MATLAB 6.5 (R13)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video