function [Ncode, Ncom, S, Q] = countcode (F)
% COUNTCODE - Count lines of a m-file
% 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.
%
% Example:
% [Ncode, Ncomments, S, Q] = countcode('countcode.m') ;
%
% See also TYPE, HELP
% Tested for Matlab R13 & Windows
% version 1.0 (feb 2006)
% (c) Jos van der Geest
% email: jos@jasen.nl
% check the input
error(nargchk(1,1,nargin)) ;
if ~ischar(F),
error(sprintf('Function ''%s'' expects a string argument.',mfilename)) ;
end
[p,f,e] = fileparts(F) ;
% make sure the input is a m-file or an asv-file
if isempty(e),
e = '.m' ;
elseif ~(strcmpi(e,'.m') || strcmpi(e,'.asv'))
error(sprintf('Function ''%s'' only defined for m-files and asv-files.',mfilename)) ;
end
if isempty(p),
p = pwd ;
end
f = fullfile(p,[f e]) ;
if ~exist(f),
error(sprintf('%s: File not found.',[f e])) ;
end
S = textread(f,'%s', 'delimiter','\n') ; % all lines
S = S(~cellfun('isempty',deblank(S))) ; % all non-empty lines
Q = ~cellfun('isempty',regexp(S,'^(\s)*%')) ;
Ncom = sum(Q) ; % commented lines
Ncode = length(S) - Ncom ; % coded lines