I assume that all your .mat files are at a certain level (one "folder" down) in the file tree. This could be modified to suit your needs, I only print out the file names to show how it can be done.
d = dir('.');
directories = dir(str);
for i = 3:length(directories)
current_dir = directories(i);
if (current_dir.isdir)
cd(current_dir.name);
fprintf(1, ' Stepped into directory %s/\n', current_dir.name)
d = dir('g*.m');
for i = 1:length(d)
fprintf(1, 'Found the file %s\n', d(i).name)
end
cd('..')
end
end
This code will print out the directories it visits and print the file names matching a criteria.
I hope this helps.
And don't write a new "answer" for a response, instead give comments on my answer, please.