omitting pointers when listing folder contents

18 ビュー (過去 30 日間)
Richard
Richard 2012 年 6 月 12 日
In order to list folder contents I use:
TopFolder = dir('E:\');
Say if I had one file within that specific folder MATLAB returns 3 structures where the name of the first two are just pointers i.e. '.' and '..'.
How is it possible to omit these? I currently just omit the first two by:
a = SubFolder(3:end)
then use
a.name
to obtain the name. Is there a more accurate way of doing this i.e. supported by the documentation?

採用された回答

Jan
Jan 2012 年 6 月 12 日
It is not documented, that . and .. are the first two elements replied by DIR. Therefore checking the names is safer:
DirList = dir('E:\');
DirName = {DirList.name};
DirList(strcmp(DirName, '.')) = [];
DirList(strcmp(DirName, '..')) = [];
To omit all files and folders starting with a dot:
DirList = dir('E:\');
DirName = {DirList.name};
DirList(strncmp(DirName, '.', 1)) = [];
Now "DirList" is the cleaned list of files and folders.
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 6 月 12 日
But will still have any folders whose names did not happen to start with '.'

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 6 月 12 日
TopFolder = dir('E:\');
TopFolder([TopFolder.isdir]) = [];

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by