gpt4 book ai didi

matlab - 如何获取 MATLAB 中特定目录中所有目录名称和/或所有文件的列表?

转载 作者:太空宇宙 更新时间:2023-11-03 20:17:05 24 4
gpt4 key购买 nike

我想做两件事:

  1. 获取目录中所有目录名称的列表,以及
  2. 获取目录中所有文件名的列表

我如何在 MATLAB 中执行此操作?

现在,我正在尝试:

dirnames = dir(image_dir);

但我认为这会返回一个对象列表。 size(dirnames)返回属性个数,dirnames.name只返回第一个目录的名称。

最佳答案

函数DIR实际上返回一个 structure array每个文件或给定目录中的子目录有一个结构元素。当getting data from a structure array , 使用点符号访问字段将返回 comma-separated list每个结构元素一个值的字段值。此逗号分隔列表可以是 collected into a vector将其放在方括号 []cell array 中将其放在花括号 {} 中。

我通常喜欢使用 logical indexing 来获取目录中文件或子目录名称的列表。 ,像这样:

dirInfo = dir(image_dir);            %# Get structure of directory information
isDir = [dirInfo.isdir]; %# A logical index the length of the
%# structure array that is true for
%# structure elements that are
%# directories and false otherwise
dirNames = {dirInfo(isDir).name}; %# A cell array of directory names
fileNames = {dirInfo(~isDir).name}; %# A cell array of file names

关于matlab - 如何获取 MATLAB 中特定目录中所有目录名称和/或所有文件的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4869188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com