dir() gives extra information

2 ビュー (過去 30 日間)
monkey_matlab
monkey_matlab 2016 年 11 月 11 日
回答済み: Walter Roberson 2016 年 11 月 11 日
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?

採用された回答

Walter Roberson
Walter Roberson 2016 年 11 月 11 日
MyFolderInfo = dir('myfolder');
mask = ismember({MyFolderInfo.name}, {'.', '..'});
MyFolderInfo(mask) = []; %get rid of . and .. directories
num_subfolder = sum( [MyFolderInfo.isdir] );

その他の回答 (1 件)

Image Analyst
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.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by