统计文件夹下所有.m文件包含的代码行数
参考代码
说明:忽略注释行和空行
codelines = reallines('./you folder path');
fprintf('code lines(omits the emtpy lines and comment lines): %d\n',codelines);
子函数reallines
function l = reallines(filepath)
if nargin == 0
filepath = pwd;
end
if iscell(filepath)
l = 0;
for i = 1:numel(filepath)
l = l + reallines(filepath{i});
end
return
end
l = 0;
if nargin == 0, filepath = cd; end
if ~exist(filepath, 'file')
error([filepath ' is not a file or directory']);
end
if exist(filepath, 'dir')
files = dir(filepath);
for i = 1:numel(files)
file = files(i);
if ~strcmp(file.name, '.') && ~strcmp(file.name, '..')
l = l + reallines([filepath, '/', file.name]);
end
end
else
if length(filepath) > 2 && filepath(end) == 'm' && filepath(end-1) == '.'
l = getfilereallines(filepath);
end
end
end
子函数getfilereallines
function l = getfilereallines(filename)
fid = fopen(filename, 'r');
if fid<0
error(['Can''t open file for reading (' filename ')'])
end
l = 0;
while ~feof(fid)
t = fgetl(fid);
for i = 1:numel(t)
if t(i) ~= ' '
break;
end
end
if numel(t) && i < numel(t) && t(i) ~= '%'
l = l + 1;
end
end
fclose(fid);
end