dir() gives extra information
1 次查看(过去 30 天)
显示 更早的评论
Hello, I wanted to just get the number of subfolders from a specific main folder. The main folder contains files, together with sub-folders. How do I get just the number of folders?
If I use
size(dir())
I get everything in the main folder. How to get just the number of sub-folders?
0 个评论
采纳的回答
Walter Roberson
2016-11-11
MyFolderInfo = dir('myfolder');
mask = ismember({MyFolderInfo.name}, {'.', '..'});
MyFolderInfo(mask) = []; %get rid of . and .. directories
num_subfolder = sum( [MyFolderInfo.isdir] );
0 个评论
更多回答(1 个)
Image Analyst
2016-11-11
编辑:Image Analyst
2016-11-11
Look up isdir() in the help. Hopefully it's obvious what to do from there.
Actually dir() tells you:
MyFolderInfo = dir('myfolder')
MyFolderInfo =
5×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
See? The structure has an "isdir" field that flags whether or not the file returned is a folder or not.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!