loading and storing specific field of many different file names of a struct

8 views (last 30 days)
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.
  3 Comments
Stephen23
Stephen23 on 23 Nov 2018
"...i was able to load all the 100 files into one struct,"
But your code does not show this at all. it shows you loading lots of separate scalar structures into one cell array (the badly named TempMatrix).

Sign in to comment.

Accepted Answer

umichguy84
umichguy84 on 23 Nov 2018
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];

More Answers (1)

Stephen23
Stephen23 on 23 Nov 2018
Edited: Stephen23 on 23 Nov 2018
Method one: dir and a structure array:
D = uigetdir();
S = dir(fullfile(D,'*.mat'));
for k = 1:numel(S)
S(k).import = load(fullfile(D,S(k).name));
end
Then you can access the data using indexing, e.g. for the second file:
S(2).import.data
Method two: sprintf and a cell array:
D = uigetdir();
N = 100;
C = cell(1,N);
for k = 1:N
F = sprintf('exp_%03d.mat',k);
C{k} = load(fullfile(D,F));
end
S = [C{:}]; % optional: convert cell array to single structure
Then you can access the imported data using indexing, e.g. for the second file:
C{2}.data
S(2).data % optional: if converted to single structure.
You should also read these:
  1 Comment
Kirankumar Thangadurai
Kirankumar Thangadurai on 23 Nov 2018
Thank you so much for all your effort, it was very useful to improve my code and sorry about the bad naming, I was just trying it out roughly.

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!