I need to list the subfolders inside a folder using Matlab. If I use
nameFolds = dir(pathFolder),
I get .
and ..
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.