MATLAB: Automatically change subfolders within a directory

change directorydata import and exportprogramming

I need to some code that will automatically run through all the folders in a directory, running the code for all the files within each folder. I am not sure how to automatically change folders within the directory. All the folders begin with 2011 but the next 16 numbers vary randomly.
Thanks

Best Answer

listing = dir;
allDirNames = { listing.name };
dirLA = [ listing.isdir ];
dirLA(1:2) = 0;
dirNames = allDirNames(dirLA);
for idx = 1:length(dirNames)
cd(dirNames{idx})
% assuming that all files in the directory to be run
fileNames = ls;
fileNames = cellstr(fileNames(3:end,:));
for id2 = 1:length(fileNames)
eval(fileNames{id2}(1:end-2))
end
cd ..
end