Stripping outputs from dir function

2 ビュー (過去 30 日間)
big ted
big ted 2020 年 5 月 28 日
コメント済み: Steven Lord 2020 年 5 月 28 日
I have a folder with a bunch of png images:
one.png
two.png
three.png etc.
I read the contents of the folder using the dir command:
fileList = dir;
which returns a structure, the name field of which contains:
'.'
'..'
'one.png'
'two.png'
etc.
I would like to remove the invalid entries generated by the Windows file structure. I am sure there is an easy way to do this!
Seems like it should be possible to do using cellfun, but
str = '.png';
fileList = fileList(cellfun(@contains str, fileList.name));
is invalid.
Creating a loop and using contains seems inefficient, but I don't have too many files:
for ii = 1:length(fileList)
idx(ii) = contains('.png',fileList(ii).name);
end
bizarrely returns idx = 1 0 0 0...
Interestingly, the contains command appears to not work as I expect:
contains('.png','one.png')
ans =
logical
0
which explains why my loop approach doesn't work...
This is a problem I face regularly, so any help would be appreciated!

採用された回答

Fangjun Jiang
Fangjun Jiang 2020 年 5 月 28 日
fileList = dir('*.png')

その他の回答 (1 件)

big ted
big ted 2020 年 5 月 28 日
You're kidding me?! I swear I've tried this before with no luck as I couldn't get dir to return an output to a variable if I also provided inputs.
Embarassing, but thanks!
  1 件のコメント
Steven Lord
Steven Lord 2020 年 5 月 28 日
You were likely calling dir in its command form.
dir *.png
You can't specify outputs in command form. For that you need function form, but that also requires you to wrap inputs that you want to pass into the function as char vectors using single quotes.
Q = dir('*.png')
See this documentation page for more information.

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

カテゴリ

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