function fileList = read_all_file_names_from_folder(folderName, fileSuffix, isRelativePath) % Read all of the file names from a special folder that contains files with % sepcial file suffix. %--------------------------- Input ----------------------------- % filderName: the name of the file folder, which should be ended with '\'. % fileSuffix: the special file suffix string. % isRelativePath: 0 or 1, which is used to identify that the output file % names to be relative path or not. %--------------------------- Output ---------------------------- % fileList: the file name list, a matlab cell data. Each row contains one % file name. %--------------------------------------------------------------- % Notice that: % (1) The path of folder should not contain any blank space, otherwise the dose command will be fail. % (2) When you use matlab with the version earlier than matlab2010b, please % replace the "strfind" function with "findstr". % % Example case: % fileList = read_all_file_names_from_folder('test\', 'txt', 0); % or fileList = read_all_file_names_from_folder('test', 'txt', 0); %# Check the input parameters if nargin == 2 isRelativePath = 0; end if strcmp(folderName(end), filesep) ~= 1 folderName = [folderName, '\']; end if strcmp(fileSuffix(1), '.') == 1 fileSuffix = fileSuffix(2:end); end %# Check the input folderName if ~isempty(strfind(folderName,' ')) disp('There are some blank space in the folder name: '); disp([' ' folderName]); disp('Please change to use the version2 instead of this one!'); end %# Run dos command to get all file name list [s, w] = dos(['dir ' folderName '*.' fileSuffix ' /s /B >fileList.txt']); fileList = importdata('fileList.txt'); dos('del fileList.txt'); %# Return the relative path or the absolute path if isRelativePath == 1 disp('Convert absolute path to related path...'); curPath = [pwd, filesep, folderName]; curSepLen = length(strfind(curPath, filesep)); if ~isempty(fileList) for i = 1:length(fileList) fPath = fileList{i}; fSepId = strfind(fPath, filesep); fileList{i} = fPath((fSepId(curSepLen)+1):end); end end end end
matlab的搜索指定类型文件,读取文件夹中全部指定类型文件Matlab代码
最新推荐文章于 2023-10-11 17:20:36 发布