如何获取MATLAB中特定目录下的所有文件? [英] How to get all files under a specific directory in MATLAB?

查看:178
本文介绍了如何获取MATLAB中特定目录下的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将所有这些文件放在 D:\dic 下,并循环使用以进一步处理。



MATLAB是否支持这种操作?



可以在其他脚本(如PHP,Python ...)中完成...

解决方案

更新:由于这篇文章很旧,而且我在这段时间内修改了这个实用程序供我自己使用,以为我应该发布一个新版本。我最新的代码可以在 MathWorks文件交换找到: dirPlus.m 。您还可以从 GitHub 获取来源。



<我做了一些改进。现在,您可以选择添加完整路径或仅返回文件名(从 Doresoom Oz Radiano )并应用常规表达式模式到文件名(从彼得D )。此外,我添加了对每个文件应用验证功能的功能,允许您根据除了其名称(即文件大小,内容,创建日期等)之外的标准来选择它们。






注意:在较新版本的MATLAB(R2016b及更高版本)中, dir 功能具有递归搜索功能!所以您可以这样做来获取当前文件夹的所有子文件夹中所有 *。m 文件的列表:

  dirData = dir('** / *。m'); 






旧代码:(用于后代)



这是一个函数,递归地浏览给定目录的所有子目录,收集所有找到的文件名的列表:

  function fileList = getAllFiles(dirName)

dirData = dir(dirName); %#获取当前目录的数据
dirIndex = [dirData.isdir]; %#查找目录的索引
fileList = {dirData(〜dirIndex).name}'; %'#获取文件列表
如果〜isempty(fileList)
fileList = cellfun(@(x)fullfile(dirName,x),...%#预设文件路径
fileList,'UniformOutput',false);
end
subDirs = {dirData(dirIndex).name}; %#获取子目录列表
validIndex =〜ismember(subDirs,{'。','..'}); %#查找子目录的索引
%#不是'。'或'..'
for iDir = find(validIndex)%#循环有效子目录
nextDir = fullfile(dirName,子目录{IDIR}); %#获取子目录路径
fileList = [fileList; getAllFiles(nextDir)]; %#递归调用getAllFiles
end

end

保存后您可以通过以下方式调用上述函数:

  fileList = getAllFiles('D:\\ \\dic'); 


I need to get all those files under D:\dic and loop over them to further process individually.

Does MATLAB support this kind of operations?

It can be done in other scripts like PHP,Python...

解决方案

Update: Given that this post is quite old, and I've modified this utility a lot for my own use during that time, I thought I should post a new version. My newest code can be found on The MathWorks File Exchange: dirPlus.m. You can also get the source from GitHub.

I made a number of improvements. It now gives you options to prepend the full path or return just the file name (incorporated from Doresoom and Oz Radiano) and apply a regular expression pattern to the file names (incorporated from Peter D). In addition, I added the ability to apply a validation function to each file, allowing you to select them based on criteria other than just their names (i.e. file size, content, creation date, etc.).


NOTE: In newer versions of MATLAB (R2016b and later), the dir function has recursive search capabilities! So you can do this to get a list of all *.m files in all subfolders of the current folder:

dirData = dir('**/*.m');


Old code: (for posterity)

Here's a function that searches recursively through all subdirectories of a given directory, collecting a list of all file names it finds:

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

After saving the above function somewhere on your MATLAB path, you can call it in the following way:

fileList = getAllFiles('D:\dic');

这篇关于如何获取MATLAB中特定目录下的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆