34

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.

Maddy
  • 2,520
  • 14
  • 44
  • 64

4 Answers4

59

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.

yuk
  • 19,098
  • 13
  • 68
  • 99
  • If you want to include all hidden subdirectories (e.g. if you work with `git`), you may use `startsWith(nameFolds, '.')` instead of `ismember(nameFolds,{'.','..'})`. – dopexxx Oct 28 '18 at 13:58
8

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.

fvrghl
  • 3,642
  • 5
  • 28
  • 36
theLtran
  • 81
  • 1
  • 1
  • 2
    This command lists the main folder as well as the subfolders -- it's not what the asker wanted. – ioanwigmore Oct 21 '13 at 12:05
  • 1
    This answer helped me in listing all sub directories quickly. Thanks. – axs Feb 25 '14 at 16:48
  • 1
    Very nice solution! But you need to remember that `genpath` function designed to generate search path, so it skips some specific folders. See the [doc](http://www.mathworks.com/help/matlab/ref/genpath.html). – yuk May 20 '14 at 15:54
  • Also, you need to use pathsep instead of hardcoding semicolon, since the path separator differs on unix than windows. – Andy Campbell Jul 05 '18 at 22:04
1

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};
Yas
  • 4,957
  • 2
  • 41
  • 24
0

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
Matthijs Noordzij
  • 923
  • 1
  • 7
  • 8