MATLAB: Guidance on using For Loops for loading data and applying function.

for loop

I have 11 directories (-2d to 8d) each containing 7 data files (100mm.dat to 700mm.dat). At present in order to load these files in a single script I am doing the following:
load ./-2d/100mm.dat
load ./-2d/200mm.dat
load ./-2d/300mm.dat
load ./-2d/400mm.dat
load ./-2d/500mm.dat
load ./-2d/600mm.dat
load ./-2d/700mm.dat
m2d1 = X100mm;
m2d2 = X200mm;
m2d3 = X300mm;
m2d4 = X400mm;
m2d5 = X500mm;
m2d6 = X600mm;
m2d7 = X700mm;
for each directory. Now this works fine, it gets the data loaded up for me to play around with but my instinct tells me that there is a better way to write this and I was thinking that it may involve a for loop, something like:
for i=1:7;
load ./-2d/(i)00mm.dat;
m2d(i) = X(i)00mm;
end
I know the above is probably nonsense to MATLAB but I am just trying to express the idea I have in my mind. Likewise for the directories would it be possible to use a for loop for that as well, something like:
for j=1:8;
for i=1:7;
load ./(j)d/(i)00mm.dat;
m2d(i) = X(i)00mm;
end
end
Thanks in advance, I hope the above makes sense.

Best Answer

for loading a dat file you can generate the string of the file as shown below and then use this string directly as input for the load function
to assign the changing variable names you will need to use eval
for i=1:7;
stringDat = ['./-2d/' num2str(i) '00mm.dat'];
load(stringDat);
eval(['m2d(i) = X' num2str(i) '00mm']);
end