File Exchange

image thumbnail


version (7.03 KB) by Boris Schauerte
This Matlab function calculates the (effective) source lines of (Matlab) code.

1 Download

Updated 26 Dec 2012

View License

This Matlab function calculates the (effective) source lines of (Matlab) code.
It is possible to call it for any function name that is in Matlab's current
search path, a filename, or a directory. If you call the function for a
directory, then recursion is used to calculate the sloc count for all files
in the folder and its sub-folders.

A function to show a sorted list of files (sorted by SLOC, ascending) is also

Please have a look into 'example.m' to see how to use the code.

It is also possible to change the function that processes individual files.
This way you can, e.g., integrate/use a C++/C or .txt handler. Currently,
there are actually two places where you can do it: First, you can modify/
extend 'calcsloc' or you completely replace 'calcsloc' with an interface-
compatible version.

Of course similar packages exist such as, e.g.,
sloc (
slocDir (
However, this code is a rewrite that features more functionality and directly
bundles the functionality of slocDir with sloc. Nevertheless, if you don't
like this package, I would suggest that you check the packages referenced
above, maybe they are more like what you are searching for.

Cite As

Boris Schauerte (2021). SLOCstat (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (4)


Nice. WAY faster that slocdir and slocDir. 2 seconds for a sample directory tree with ~1600 files in dozens of subfolders vs. > 40 seconds with slocdir.
But, has bug for handling of continued lines (need to assign pline = tline) and doesn't handle block comments %{ ... %}. These two issues can be easily remedied.


I have tried other similar programs (codemetrics, program_statistics, slocDir, sloc) and SLOCstat is the best among them. It is the quickest and the results are the easiest to understand. It seems to loop through all files in all subfolders automatically. An option to exclude some subfolders/files would be very useful but not essential because it is easy to remove them from the printed output using a spreadsheet.

Craig Cole

Don't need perfection, just good relative estimates, and this did the trick simply.


Nice work. This is exactly what I needed for my project. Maybe some annotations:
- Comments behind code are not counted
- Multiple statements on one line are not considered
- The example uses "dct" command of the Signal Processing Toolbox, using something like "repmat" would be working on more installations, though

MATLAB Release Compatibility
Created with R2012b
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!