Thursday, April 2, 2015

[MATLAB] How to get a list of folders (not files)

Code example

list_fold = dir(path_fold);

% Find only folders
idx_fold = [list_fold(:).isdir];

% Select only folders
name_fold = {list_fold(idx_fold).name}';

% Remove '.' and '..'
name_fold( ismember(name_fold, {'.','..'}) ) = [];



NOTE that if you delete '.' and '..' like as follows

name_fold(1:2) = [];

it might cause problem since 'dir' output from root directory does not contain '.' and '..' at least on Windows OS.

No comments:

Post a Comment