Simply Directory Question

Hello All,
Quick question, I am trying to load all files from a folder that have a .m ext:
folder = C:\Users\ecorbett\Documents\MATLAB\UI\VCoachData\0005_Capture_10072009\AccelData
files = dir(folder)
I know I need to include something like '*.m' in dir but I dont know the correct syntax to do so. Can someone help me with this?

 채택된 답변

Paulo Silva
Paulo Silva 2011년 8월 4일

1 개 추천

m=dir([folder '\*.m']);
files=arrayfun(@(x)m(x).name,1:numel(m),'uni',false); %or use Oleg suggestion

댓글 수: 9

Oleg Komarov
Oleg Komarov 2011년 8월 4일
files = {m.name};
Paulo Silva
Paulo Silva 2011년 8월 4일
Thanks Oleg, I always forget that easy way to do it
B_Richardson
B_Richardson 2011년 8월 4일
what is this "name?" Is that my "folder"?
Oleg Komarov
Oleg Komarov 2011년 8월 4일
Check out the documentation for dir. Name is a field of the structure m.
Walter Roberson
Walter Roberson 2011년 8월 4일
No, in this context, "name" is a particular field from the structure returned by dir. m(K).name is the name of the K'th file, but without any parts of the folder name.
Paulo Silva
Paulo Silva 2011년 8월 4일
name is a field of the structure you get from the dir function, each file the dir function accepts creates a new entry on the structure with several fields, name being one of them.
B_Richardson
B_Richardson 2011년 8월 4일
I see! Thank you all! I feel like you guys are my professors sometimes lol
Walter Roberson
Walter Roberson 2011년 8월 4일
Think I'd be able to get Tenure? ;-)
B_Richardson
B_Richardson 2011년 8월 4일
You'd have my vote!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

B_Richardson
B_Richardson 2011년 8월 5일

0 개 추천

Hey guys, I'm still having trouble with this code. I've included the some more to give you a better understanding of what I'm tring to do.
I just want to get a dir (load .mat data files only) and loop through that dir until I match my pattern. Once I match, load construct fullfile.
[pathstr, name, ext] = fileparts(file)
name = 'AccelData';
file2 = fullfile(pathstr,[name])
z=dir([file2 '\*.m']);
files = {z.name}
Pattern=itemselected(1:9)
for i = 1 : numel(files)
value=files(i).name;
if strfind(value,Pattern)
f = fullfile(file2,value)
For some reason,
z=dir([file2 '\*.m']);
files = {z.name}
files is coming up empty. The way I had it before worked:
files = dir(folder)
But of course it included all file types and i just want the matlab data files.

댓글 수: 4

Oleg Komarov
Oleg Komarov 2011년 8월 5일
z=dir([file2 '*.m']);
without the \
B_Richardson
B_Richardson 2011년 8월 5일
Still comes up empty.
z=dir([file2])
works. But the'*.m' is messing it up
B_Richardson
B_Richardson 2011년 8월 5일
0x1 struct array with fields:
name
date
bytes
isdir
datenum
Oleg Komarov
Oleg Komarov 2011년 8월 5일
can you show what's file2?

댓글을 달려면 로그인하십시오.

B_Richardson
B_Richardson 2011년 8월 5일

0 개 추천

I feel so stupid right now! My files are .mat not .m! AARRRGGGGGG Thanks everybody!!

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by