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

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

问题描述

我需要在 D:dic 下获取所有这些文件并循环遍历它们以单独进一步处理.

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

MATLAB 是否支持这种操作?

Does MATLAB support this kind of operations?

它可以在其他脚本中完成,如 PHP、Python...

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

推荐答案

更新: 鉴于这篇文章已经很老了,而且在那段时间里我对这个实用程序做了很多修改以供自己使用,我想我应该发布一个新版本.我的最新代码可以在 The MathWorks File Exchange 上找到:dirPlus.m.您还可以从 GitHub 获取源代码.

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.

我做了一些改进.它现在为您提供了添加完整路径或仅返回文件名的选项(合并自 DoresoomOz Radiano) 并应用常规文件名的表达式模式(合并自 Peter D).此外,我还添加了对每个文件应用验证功能的功能,允许您根据名称以外的标准(即文件大小、内容、创建日期等)来选择它们.

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.).

注意:在较新版本的 MATLAB(R2016b 及更高版本)中,dir 函数具有递归搜索功能!因此,您可以执行此操作以获取当前文件夹的所有子文件夹中所有 *.m 文件的列表:

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

将上述函数保存到 MATLAB 路径的某个位置后,您可以通过以下方式调用它:

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天全站免登陆