MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何获取MATLAB中特定目录中所有目录名称和/或所有文件的列表? (https://www.labfans.com/bbs/showthread.php?t=26262)

poster 2019-12-14 20:13

如何获取MATLAB中特定目录中所有目录名称和/或所有文件的列表?
 
我想做两件事:
[LIST=1][*]获取目录中所有目录名称的列表,然后[*]获取目录中所有文件名的列表[/LIST]如何在MATLAB中执行此操作?

现在,我正在尝试:

dirnames = dir(image_dir); 但我认为那会返回对象列表。 size(dirnames)返回属性的数量, dirnames.name仅返回第一个目录的名称。



[B]回答:[/B]

函数[URL="http://www.mathworks.com/help/techdoc/ref/dir.html"]DIR[/URL]实际上返回一个[URL="http://www.mathworks.com/help/techdoc/matlab_prog/br04bw6-38.html"]结构数组[/URL] ,其中给定目录中的每个文件或子目录具有一个结构元素。当[URL="http://www.mathworks.com/help/techdoc/matlab_prog/br04bw6-38.html#br1v5pf-1"]从结构数组中获取数据时[/URL] ,使用点符号访问字段将返回以[URL="http://www.mathworks.com/help/techdoc/matlab_prog/br2js35-1.html"]逗号分隔[/URL]的字段值[URL="http://www.mathworks.com/help/techdoc/matlab_prog/br2js35-1.html"]列表[/URL] ,每个结构元素一个值。将该逗号分隔的列表放在方括号[]中,可以将其[URL="http://www.mathworks.com/help/techdoc/math/f1-84864.html"]收集到向量中[/URL] ;也可以通过将其放在花括号{} ,将其存储在[URL="http://www.mathworks.com/help/techdoc/matlab_prog/br04bw6-98.html"]单元格数组[/URL]中。

我通常喜欢通过使用[URL="http://www.mathworks.com/help/techdoc/math/f1-85462.html#bq7egb6-1"]逻辑索引[/URL]来获得目录中文件或子目录名称的列表,如下所示:

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

[url=https://stackoverflow.com/questions/4869188]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:36

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.