matlab的搜索指定类型文件,读取文件夹中全部指定类型文件Matlab代码

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值