MATLAB: Problem with looping through Folder finding pattern

filesloopingpattern recognition

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.

Best Answer

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