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

来源:互联网 发布:淘宝退款率高怎么办 编辑:程序博客网 时间:2024/05/17 06:57

说明:

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=1isfile=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    endend

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    endend
0 0
原创粉丝点击