Problem with looping through Folder finding pattern

Hello everyone,
I am attempting to loop through a folder using this code:
files = dir(MyFolder);
Pattern = MyPattern % im searching for files that match a certain pattern
for i = 1 : numel(Myfolder) %loop through Myfolder
% this next line below is where the error is coming from
value = getfield(files,{i},'name') %iterate through each file in the structure, "files"
% i did this because I need a string to use in strfind below
if strfind(value,Pattern) %check to see if current file matches Pattern
f = fullfile(MyFolder,value) %construct fullpath
% do more stuff
end
end
Im not sure if this is the best approach, but the idea is to loop through MyFolder and construct a fullfile if a file matches pattern.
This is the error generated. It comes from getfield:
??? Index exceeds matrix dimensions.
Error in ==> getfield at 46
f = f(index{:});
Error in ==> UI>pushbutton2_Callback at 252
value = getfield(files,{i},'name')
Any advice on a better approach would be great.

 採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 7 月 18 日

2 投票

value=getfield(files(i),'name') or
value=files(i).name

8 件のコメント

B_Richardson
B_Richardson 2011 年 7 月 18 日
It loops and does eventually find and construct the file with these changes:
f =
C:\Users\ecorbett\Documents\VCoachData\0006_Capture_10142009\AccelData\2009-10-14 11-13-04_RHA_LL_S3_0006FinalData.mat
However, I still get this error for some reason:
??? Index exceeds matrix dimensions.
Error in ==> UI>pushbutton2_Callback at 252
value=files(i).name
I guess I could just ignore it? Since it works now?
Fangjun Jiang
Fangjun Jiang 2011 年 7 月 18 日
should this line for i = 1 : numel(Myfolder)
be for i = 1 : numel(files)?
B_Richardson
B_Richardson 2011 年 7 月 18 日
uhhhh... Maybe? My own code is confusing me now.
i = 1 : numel(Myfolder) %loops through all of the files with MyFolder
the only time files is used is here:
value = getfield(files,{i},'name')
using i to index the struct, files? right? (i think) retrieving name?
I will try you suggestion though.
B_Richardson
B_Richardson 2011 年 7 月 18 日
Wow that worked perfectly! I dont quite understand why/how but thanks alot! (an explanation would be great though!)
Fangjun Jiang
Fangjun Jiang 2011 年 7 月 18 日
if MyFolder='c:\MyDocument';
numel(MyFolder) would be 13, that is the number of characters in your folder string. To loop through all the files under that folder, you need to use numel(files).
B_Richardson
B_Richardson 2011 年 7 月 18 日
wow, are you telling me that I was looping through the actual string in MyFolder: "C:\Users\ecorbett\Documents\VCoachData\0006_Capture_10142009\AccelData"
Instead of the contents of the folder?
Fangjun Jiang
Fangjun Jiang 2011 年 7 月 18 日
That is probably true. But remember, all that "for i = 1 : numel(Myfolder)" statement does is to generate a vector i=1:70 since your folder string has 70 characters. If you happen to have 70 or more files in that folder, you won't have any problem at all (although it is still not right because the rest of the file names are not searched). But if you have less than 70 files, then files(70).name is nowhere to find. That is the error you got.
Fangjun Jiang
Fangjun Jiang 2011 年 7 月 18 日
to help, just type numel(MyFolder) and numel(files) in command window to find its value.

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2011 年 7 月 18 日

0 投票

Also if the pattern is sufficiently simple you might be able to code it using wildcards in the dir() itself. The patterns accepted on Linux and MacOS systems are usually those expressible as pure regular expressions, which is often more than enough.

3 件のコメント

B_Richardson
B_Richardson 2011 年 7 月 18 日
the fullfile is:
C:\Users\ecorbett\Documents\VCoachData\0006_Capture_10142009\AccelData\2009-10-14 11-13-04_RHA_LL_S3_0006FinalData.mat
the patterns come in these forms:
RHA_LL_S3
RHA_LL_S2
RHA_LL_S1
RHA_RL_S3
RHA_RL_S2
RHA_RL_S1
SHA_LL_S3
SHA_LL_S2
SHA_LL_S1
and so on
B_Richardson
B_Richardson 2011 年 7 月 18 日
What is a wildcard?
Walter Roberson
Walter Roberson 2011 年 7 月 18 日
For example, in Unix-type systems you could match against
dir('2009-10-14 11-13-04_[RS]HA_[LR]L_S[123]_0006FinalData.mat')
That would overshoot a little in that it would match SHA_RL_S1 (for example) which is not one of the patterns in your list, but a fair bit of the time that would be "close enough".
In MS Windows, I do not know how finely it can be controlled, but I know you could use (e.g)
dir('2009-10-14 11-13-04_?HA_?L_S?_0006FinalData.mat')
each ? would match a single character.

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by