MATLAB 读子文件夹下文件

本文介绍了一种递归算法,用于遍历指定文件夹及其子文件夹中的所有指定类型的文件,并将这些文件的完整路径保存到输出文件中。该算法首先检查主路径中的每个条目,忽略特殊目录如'.'和'..',对于每个子目录,它递归地调用自身以处理更深的嵌套文件夹。对于匹配文件类型的文件,它们的路径被记录下来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

把符合的文件路径保存到fp中,FileType为指定文件的类型,MainPath为文件夹的路径

function Path=ReadFile(fp,MainPath,FileType)
%%%%%%%%%%%%%%
%MainPath为主路径,FileType为需要读取的文件类型
PathFileFormation=dir(MainPath);
PathNumber=numel(PathFileFormation);
for LoopPathNumber=1:PathNumber

if( isequal( PathFileFormation( LoopPathNumber ).name, '.' ) || ...
isequal( PathFileFormation( LoopPathNumber ).name, '..' )) % 如果不是目录跳过
continue;
elseif PathFileFormation( LoopPathNumber ).isdir
Path=fullfile(MainPath,PathFileFormation(LoopPathNumber).name);
Path=ReadFile(fp,Path,FileType);
else

Path=fullfile(MainPath,['*',FileType]);
images=dir(Path);
for i=1:numel(images)
fprintf(fp,'%s\n',fullfile(MainPath,images(i).name));
end
break;
end

end

 

转载于:https://www.cnblogs.com/matrix314/p/5625361.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值