Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
我需要将所有这些文件放在D:\dic并循环遍历,以分别进行进一步处理。
MATLAB是否支持这种操作? 可以在其他脚本中完成,例如PHP,Python ... 回答: 更新:鉴于这篇文章已经很老了,并且在此期间我已经对该实用程序进行了很多修改以供自己使用,所以我认为我应该发布一个新版本。我的最新代码可以在The MathWorks File Exchange上找到: dirPlus.m 。您也可以从GitHub获取源代码。 我做了一些改进。现在,它为您提供了添加完整路径或仅返回文件名(由Doresoom和Oz Radiano合并 )以及将正则表达式模式应用于文件名(由Peter D合并)的选项。此外,我还添加了对每个文件应用验证功能的功能,使您可以根据标准(而不是文件名,文件大小,内容,创建日期等)选择文件。 注意:在较新版本的MATLAB(R2016b和更高版本)中, dir函数具有递归搜索功能!因此,您可以执行以下操作以获取当前文件夹的所有子文件夹中所有*.m文件的列表: dirData = dir('**/*.m'); 旧代码:(用于后代) 这是一个函数,它以递归方式搜索给定目录的所有子目录,并收集找到的所有文件名的列表: function fileList = getAllFiles(dirName) dirData = dir(dirName); %# Get the data for the current directory dirIndex = [dirData.isdir]; %# Find the index for directories fileList = {dirData(~dirIndex).name}'; %'# Get a list of the files if ~isempty(fileList) fileList = cellfun(@(x) fullfile(dirName,x),... %# Prepend path to files fileList,'UniformOutput',false); end subDirs = {dirData(dirIndex).name}; %# Get a list of the subdirectories validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories %# that are not '.' or '..' for iDir = find(validIndex) %# Loop over valid subdirectories nextDir = fullfile(dirName,subDirs{iDir}); %# Get the subdirectory path fileList = [fileList; getAllFiles(nextDir)]; %# Recursively call getAllFiles end end 将上述函数保存在MATLAB路径中的某个位置后,可以通过以下方式调用它: fileList = getAllFiles('D:\dic'); 更多&回答... |
![]() |
![]() |