Windows与Linux平台下Matlab遍历文件夹下子文件夹及文件

说明:

Matlab目录操作
1. fullfile 用于将若干字符串连接成一个完整的路径,根据不同的操作系统自动填充目录分割符。例如:

f=fullfile('D:','Matlab','test.txt')
f=D:\Matlab\test.txt

2.isdir 判断一个路径是否代表了一个目录,例如:

path='D:\Matlab';
f=fullfile(path,'test.txt');
isdir=isdir(path)
isdir=1
isfile=isdir(file)
isfile=0

3.dir 用于列出一个目录的内容,返回值为结构体数组类型,包含如下部分:name:文件或目录的名称;date:修改日期;bytes:文件大小;isdir:是否是目录。例如:

path='D:\Matlab';
files=dir(path)
files = 8x1 struct array with fields:
    name
    date
    bytes
    isdir

Windows版本:

path = 'F:/test_file/'; 
Files = dir(fullfile( path,'*.*'));
LengthFiles = length(Files);
for i = 1:LengthFiles
    % 判断是否是文件夹
    name = Files(i).name;
    if name=='.'
        continue;
    end

    s = [path  name '/'];
    fprintf('---%s\n',name);

    %遍历指定的文件类型(*.*所有类型)
    Folders = dir(fullfile( s,'*.*'));
    Length= length(Folders);

    for i = 1:Length;
       % fprintf('name:%s\n',Folders(i).name);          
        if (isequal( Folders(i).name, '.' )||isequal( Folders(i).name, '..'))  
                %continue;      
        else              
            fprintf(' |---%s\n',[s Folders(i).name]);
        end
    end
end

Linux版本:

path = './test_file/'; 

Files = dir(fullfile( path,'/'));
LengthFiles = length(Files);
for i = 1:LengthFiles
    % 判断是否是文件夹
    name = Files(i).name;
    if name=='.'
        continue;
    end

    s = [path  name '/'];
    fprintf('---%s\n',s);

     %遍历指定的文件类型(*.*所有类型)
    Folders = dir(fullfile( s,'*.*'));
    Length= length(Folders);

    for i = 1:Length;
        %fprintf('name:%s\n',Folders(i).name);          
        if (isequal( Folders(i).name, '.' )||isequal( Folders(i).name, '..'))  
                %continue;      
        else              
            fprintf(' |---%s\n',[s Folders(i).name]);
        end
    end
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sun-Eve

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值