Matlab批量获取子文件夹与文件名

一、批量得到父文件夹下所有子文件夹的路径的MATLAB函数

function [SubFolders] = GetFolders(ParentFolder)
%GetFolders
% 函数功能为获取父文件夹下所有子文件夹的路径
% 函数的输入为ParentFolder:父文件夹路径。eg: 'D:\Program Files'
% 函数的输出为SubFolders:子文件夹路径。为一个元胞数组,eg: {'D:\Program Files\FileZilla FTP Client\docs'}
  
SubFolderNames = dir(ParentFolder);
for i=1:length(SubFolderNames)
        if( isequal( SubFolderNames( i ).name, '.' )||...
        isequal( SubFolderNames( i ).name, '..')||...
        ~SubFolderNames( i ).isdir) % 如果不是目录则跳过
            continue;
        end
        SubFolder(i).SubFolderName = fullfile( ParentFolder, SubFolderNames( i ).name );
end
 
temp = {SubFolder.SubFolderName};
idx = cellfun(@(x)~isempty(x),temp,'UniformOutput',true); % 利用cellfun函数得到元胞数组中所有非空元素的下标
SubFolders = temp(idx);
 
end

二、批量得到文件夹下某一类型的所有文件名的MATLAB函数

function [FileNames] = GetFileNames(Path,Format)
% GetFileNames
% 函数的功能为获得某一路径下,某种格式所有文件名
% 函数的输入1为Path,要获取的路径。eg: 'D:\Program Files\FileZilla FTP Client\docs'
% 函数的输入2为Format,要获取路径的文件格式。eg: '*.txt','*.docx','*.png'
 
fileFolder=fullfile(Path);
dirOutput=dir(fullfile(fileFolder,Format));
FileNames={dirOutput.name};
end

利用上面两个函数,结合实际问题,利用循环便可实现文件夹嵌套情况下的文件名批量获取。

转载自:
作者:Yuhao Pan
出处:https://www.cnblogs.com/yhpan/

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值