MATLAB: Loading and storing specific field of many different file names of a struct

file namesfor loopsstruct

Hi everyone, I have a set of 100 readings from a test rig, all are multilevel struct .mat files. I am trying to group a specific field (which is common within all the files) into a single row matrix (100,:). So from the help of the Mathworks community i was able to load all the 100 files into one struct, but my real objective is to get a specific field,
folder_name=uigetdir();
filenameExtension='.mat';
TempMatrix=cell(100,1);
for n=1:100
filename=[folder_name,'\exp_0',int2str(n),filenameExtension];
TempMatrix{n,:}=load(filename);
end
all the files i want to load are named as exp_01.mat, exp_02.mat…….exp_0100.mat
the only way to accest the file I want is like this:
example for exp_02:
FinalMatrix=[TempMatrix(2).data.exp_02.Y(3).Data];
so u see i need to change the file name each time to get the answer. But how do i get it in a for loop to store all the 100 files target field(Y(3)) into a single array matrix for ease of access.
Any help/advice would be realy appreciated. Thank you.
iv attached few example files.

Best Answer

When using structures you can use variables and expressions to access fields using () . For instance lets say you had the following.
x.b1=1;
x.b2=2;
y = zeros(2,1)
% You can access the fields in a loop by using ()
for i=1:2
y(i)=x.(['b' num2str(i)]);
end
So assuming I'm understanding your problem correctly you could do:
filename = ['exp_0',int2str(n)];
FinalMatrix=[TempMatrix(2).data.(filename).Data];