说明:
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