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

后端 未结 4 1861
心在旅途
心在旅途 2020-12-25 12:13

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

nameFolds = dir(pathFolder), 

I get . and ..

4条回答
  •  春和景丽
    2020-12-25 12:47

    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 parentdir in 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.

提交回复
热议问题