Matlab代码行数统计

统计文件夹下所有.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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值