Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 738
Maddy Avatar asked Jan 05 '12 20:01

Maddy


People also ask

How do I see all files in a subfolder?

Enter the main folder you want to see and Ctrl + B . That will list all files within the main folder and all of its subfolders.

How do I list files in a directory in MATLAB?

To search for multiple files, use wildcards in the file name. For example, dir *. txt lists all files with a txt extension in the current folder. To search through folders and subfolders on the path recursively, use wildcards in the path name.

How do I add all the subfolders to a path in MATLAB?

Add Folder and Its Subfolders to Search Path Add matlab/myfiles and its subfolders to the search path. Create the folder matlab/myfiles and call genpath inside of addpath to add all subfolders of matlab/myfiles to the search path.


1 Answers

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.

like image 69
yuk Avatar answered Sep 21 '22 11:09

yuk