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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With