Why does dir('*.mat') not list all of the .mat files in directory?

Hello,
I'm trying to create a structure file which lists all of the .mat files in my directory:
files = dir('*.mat')
This code returns a 4x1 structure, but I have 8 .mat files in the directory. All 8 of the .mat files are the same format (i.e. 3 variables per .mat file). Can anyone tell me why my other .mat files are not being listed in the struct file?
Thanks, Dan

 採用された回答

Matt Fig
Matt Fig 2012 年 8 月 21 日
編集済み: Matt Fig 2012 年 8 月 21 日

0 投票

Are you sure about those files being in there? Does WHAT see them (type: what) in there?
D = dir;
length(regexp([D(:).name],'\.mat')) % What does this say

6 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 21 日
length(regexp([D.name],'\.mat')) % w:hy D(:)?
Dan
Dan 2012 年 8 月 21 日
Matt,
The "what" command shows the names of all 8 files.
D = dir;
length(regexp([D(:).name],'\.mat')) % What does this say
This code gives an answer of 8. I tried files = dir('*.mat') several times and still only get 4 files listed.
Thanks, Dan
Matt Fig
Matt Fig 2012 年 8 月 21 日
Azzi, No reason. Just my mental notation... ;-)
Matt Fig
Matt Fig 2012 年 8 月 21 日
I have no idea why this DIR isn't finding your files. Since WHAT is, why not just take:
W = what(pwd);
files = W.mat
Dan
Dan 2012 年 8 月 21 日
Matt, your example above works just fine...now its just bugging me why DIR isn't working.
Thanks!
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 21 日
nice

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 21 日
編集済み: Azzi Abdelmalek 2012 年 9 月 1 日

0 投票

try this
d=dir
d1=struct2cell(d);
d2=d1(1,:);
d3=cellfun(@(x) regexp(x,'.mat'),d2,'uni',false)
d4=find(~cellfun('isempty', d3))
files=d2(d4)'

3 件のコメント

Dan
Dan 2012 年 8 月 21 日
Azzi, your work almost works for me, but 'files' then contains one .png that is in my directory.
Jan
Jan 2012 年 8 月 22 日
~cellfun('isempty', d3) is faster than ~cellfun(@isempty, d3), which is faster than cellfun(@(x) ~isempty(x)).
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 1 日
thanks Simon, did'nt read your comment until now.

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

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

タグ

質問済み:

Dan
2012 年 8 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by