File Exchange

image thumbnail

COUNTCODE

version 1.0 (1.71 KB) by

count coded and commented lines in an m-file

2 Downloads

Updated

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

Pavan (view profile)

Nice and useful function.

sri harsha boddepalli

Raymond Norris

Jos-

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 (http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=3900&objectType=file) 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)
us

MATLAB Release
MATLAB 6.5 (R13)

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

» Watch video