MATLAB: How to read directory for folders with numbers in Matlab

data importload

Hi,
I am trying to process data from almost 100 folders containing my data. The name of the folders is like part1, part2,…, part100 and each folder contains 4 .mat files. It is also important to know the order of the folders. How can I read these 100 data folders in a for loop? I tried the following code:
for i=1:100
A(i)= load('part',num2str(i),'\....mat');
end
but it did not work.
Thanks, Fatemeh

Best Answer

A = cell(4, 100);
BaseFolder = 'C:\YourData';
for iFolder = 1:100
for iFile = 1:4
A(iFile, iFolder) = load(fullfile(BaseFolder, sprintf('part%d', iFolder), ...
sprintf('File%d.mat', iFile));
end
end