function returnFiles=getFiles(baseDir)
% get files in a directory
%input baseDir: is the directory for get Files,it must be a string end with
%'/'
%output returnFiles:is a cell of file paths in the directory
files=dir(baseDir);
%m is the numbers of the files contained in the directory
[m,tmp]=size(files);
%returnFiles is a cell for storing the file paths
%it is set to size of 1 and storing 'Files are:' for the state that there is no files
%in the directory
returnFiles=cell(1);
returnFiles{1}='Files are:';
for i=1:m
%processing erery file
current=files(i);
%get the file name
file_name=current.name;
%processing with the state of '.' and '..'
if strcmp(file_name,'.')==1 || strcmp(file_name,'..')==1
continue;
end
if current.isdir==1
% if it is a directory
% get the diretory path
currentDir=strcat(baseDir,strcat(file_name,'/'));
%processing the sub direcotry
tmpReturnFiles=getFiles(currentDir);
%save the files contained in the subdir to the cell of returnFiles
%processing with the state that there is no file in the subdir
if strcmp(tmpReturnFiles{1},'Files are:')==0
%processing with the state that there is no file stored in the
%cell of returnFiles
if strcmp(returnFiles{1},'Files are:')==1
returnFiles=tmpReturnFiles;
else
%merge the files in current dir and the files in the subdir
returnFiles=[returnFiles,tmpReturnFiles];
end
end
else
% get the file path of the file
filePath=strcat(baseDir,file_name);
%to find that if there is any file stored in the cell
if strcmp(returnFiles{1},'Files are:')==1
% if there is no file stored in the cell,than save current file
% to the first cell
returnFiles{1}=filePath;
else
% if there is any file stored in the cell,than merge
returnFiles=[returnFiles,{filePath}];
end
end
end
% get files in a directory
%input baseDir: is the directory for get Files,it must be a string end with
%'/'
%output returnFiles:is a cell of file paths in the directory
files=dir(baseDir);
%m is the numbers of the files contained in the directory
[m,tmp]=size(files);
%returnFiles is a cell for storing the file paths
%it is set to size of 1 and storing 'Files are:' for the state that there is no files
%in the directory
returnFiles=cell(1);
returnFiles{1}='Files are:';
for i=1:m
%processing erery file
current=files(i);
%get the file name
file_name=current.name;
%processing with the state of '.' and '..'
if strcmp(file_name,'.')==1 || strcmp(file_name,'..')==1
continue;
end
if current.isdir==1
% if it is a directory
% get the diretory path
currentDir=strcat(baseDir,strcat(file_name,'/'));
%processing the sub direcotry
tmpReturnFiles=getFiles(currentDir);
%save the files contained in the subdir to the cell of returnFiles
%processing with the state that there is no file in the subdir
if strcmp(tmpReturnFiles{1},'Files are:')==0
%processing with the state that there is no file stored in the
%cell of returnFiles
if strcmp(returnFiles{1},'Files are:')==1
returnFiles=tmpReturnFiles;
else
%merge the files in current dir and the files in the subdir
returnFiles=[returnFiles,tmpReturnFiles];
end
end
else
% get the file path of the file
filePath=strcat(baseDir,file_name);
%to find that if there is any file stored in the cell
if strcmp(returnFiles{1},'Files are:')==1
% if there is no file stored in the cell,than save current file
% to the first cell
returnFiles{1}=filePath;
else
% if there is any file stored in the cell,than merge
returnFiles=[returnFiles,{filePath}];
end
end
end
本文介绍了一个用于Matlab的自定义函数getFiles,该函数能够递归地获取指定目录及其子目录下的所有文件路径,并将这些路径保存在一个单元数组中返回。通过对当前文件和子目录的判断及处理,确保了所有文件都能被正确地记录。
177

被折叠的 条评论
为什么被折叠?



