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


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

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.

Best Solution

Use isdir field of dir output to separate subdirectories and files:

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 dir output from root directory does not contain those dot-folders. At least on Windows.