Code covered by the BSD License  

Highlights from
sloc

4.0

4.0 | 5 ratings Rate this file 36 Downloads (last 30 days) File Size: 2.36 KB File ID: #3900

sloc

by Raymond Norris

 

22 Aug 2003 (Updated 09 Mar 2006)

Count lines of source code for an M-file

| Watch this File

File Information
Description

Counts the lines of source code in an M-file.

A line of source code is defined to be not a comment line, not a continuation of a previous line, and not an empty line.

The function line is included. SLOC returns the line count for the entire file, not for individual functions of a file.

If more than more statement is on the same line, that it treated as one line. For example, the line:

   minx = 32; maxx = 100;

is one line of source code.

Acknowledgements
This submission has inspired the following:
slocdir, slocDir
MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (8)
21 Mar 2005 Andrej Mosat

Cool, I`d expect one more thing: to count all the lines for *.m files in the current directory recursively. Thanx, no rating yet.

21 Mar 2005 Andrej Mosat

I`d add:
line 32: file=deblank(file);
or similar, because:
files=ls('*.m')
returns a char array with spaces at the end.
after:
count=count+sloc(files(i,:))
error occurs: 'xyz.m .m' cannot be found
But spaces in the middle of file would need a regexp.

01 Mar 2006 Jiro Doke

Good how you incorporated multi-line codes continued by <...>. But some commands can have multiple lines without <...>, such as mutli-row matrix definition. But I guess this will be hard to detect.

18 Jul 2007 Gregory Wunder

I have already developed a similar program. God Bless!

04 Dec 2007 Dionisio Arcano  
05 May 2009 Diffy D

I made a little script to list all the .m recursively and count the number of lines with sloc, you can fount it at : http://www.mathworks.com/matlabcentral/fileexchange/24000

Bye

25 Oct 2011 David Roberts

I created a function slocDir that uses this file to give sloc totals for each folder in a directory tree. Kind of confusing that there are two files out there slocdir & slocDir which seem to do the same thing. slocDir was submitted to Matlab Central a few weeks before slocdir.

04 Nov 2011 Mike  
Please login to add a comment or rating.
Updates
04 Oct 2004

Updated to support R14, specifically for block comments ( {%...}% )

09 Mar 2006

Bug fixes:
(1) The MATLAB Profiler does not include the "function" line in it's metrics, so do not count the "function" line or any "function" lines for subfunctions. (2) Also, functions (particulary nested) may be indented, so minor fix for that.

Tag Activity for this File
Tag Applied By Date/Time
sloc Raymond Norris 22 Oct 2008 07:06:47
source lines of code Raymond Norris 22 Oct 2008 07:06:47
line count Raymond Norris 22 Oct 2008 07:06:47
kloc Raymond Norris 22 Oct 2008 07:06:47

Contact us at files@mathworks.com