list 列出文件夹中的子文件夹 - Matlab(仅子文件夹,而不是文件)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8748976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:45:53  来源:igfitidea点击:

list the subfolders in a folder - Matlab (only subfolders, not files)

listmatlabdirectorydir

提问by Maddy

I need to list the subfolders inside a folder using Matlab. If I use

我需要使用 Matlab 列出文件夹内的子文件夹。如果我使用

nameFolds = dir(pathFolder), 

I get .and ..+ the subfolder names. I then have to run nameFolds(1) = []twice. Is there a better way to get the subFolder names using Matlab? Thanks.

我得到...+ 子文件夹名称。然后我必须跑nameFolds(1) = []两次。有没有更好的方法来使用 Matlab 获取子文件夹名称?谢谢。

回答by yuk

Use isdirfield of diroutput to separate subdirectories and files:

使用输出isdir字段dir来分隔子目录和文件:

d = dir(pathFolder);
isub = [d(:).isdir]; %# returns logical vector
nameFolds = {d(isub).name}';

You can then remove .and ..

然后您可以删除...

nameFolds(ismember(nameFolds,{'.','..'})) = [];

You shouldn't do nameFolds(1:2) = [], since diroutput from root directory does not contain those dot-folders. At least on Windows.

您不应该这样做nameFolds(1:2) = [],因为dir根目录的输出不包含那些点文件夹。至少在 Windows 上。

回答by theLtran

This is much slicker and all one line:

这是更光滑的一行:

dirs = regexp(genpath(parentdir),['[^;]*'],'match');

Explained: genpath()is a command which spits out all subfolders of the parentdirin a single line of text, separated by semicolons. The regular expression function regexp()searches for patterns in that string and returns the option: 'matches' to the pattern. In this case the pattern is any character not a semicolon = `[^;], repeated one or more times in a row = *. So this will search the string and group all the characters that are not semicolons into separate outputs - in this case all the subfolder directories.

解释: genpath()是一个命令,它parentdir在一行文本中吐出 的所有子文件夹,用分号分隔。正则表达式函数regexp()在该字符串中搜索模式并返回选项:“匹配”到模式。在这种情况下,模式是任何不是分号的字符 = `[^;],连续重复一次或多次 = *。因此,这将搜索字符串并将所有不是分号的字符分组到单独的输出中 - 在这种情况下是所有子文件夹目录。

回答by Yas

The following code snippet just returns the name of sub-folders.

以下代码片段仅返回子文件夹的名称。

% `rootDir` is given
dirs = dir(rootDir);
% remove `.` and `..`
dirs(1:2) = [];
% select just directories not files
dirs = dirs([obj.dirs.isdir]);
% select name of directories
dirs = {dirs.name};

回答by Matthijs Noordzij

And to effectively reuse the first solution provided in different scenario's I made a function out of it:

为了有效地重用不同场景中提供的第一个解决方案,我做了一个函数:

function [ dirList ] = get_directory_names( dir_name )

    %get_directory_names; this function outputs a cell with directory names (as
    %strings), given a certain dir name (string)
    %from: http://stackoverflow.com/questions/8748976/list-the-subfolders-
    %in-a-folder-matlab-only-subfolders-not-files

    dd = dir(dir_name);
    isub = [dd(:).isdir]; %# returns logical vector
    dirList = {dd(isub).name}';
    dirList(ismember(dirList,{'.','..'})) = [];

end