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>
Nice and useful function.
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.
An update (with which and total lines) is on its way. Unfortunately, I do not have R14 yet ... Poor me ...
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.
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.
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)
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.