Concatenate same fields in multiple structures in a loop using field names

7 views (last 30 days)
I have several .mat files and each is a structure with the same fields. For each field, I'd like to create an array that has the name of that field and is the concatenation of all structures with that field name. I can provide a psuedo-code, but not sure how to write it out for real:
firststruct has field names 'one' and 'two'
secondstruct has field names 'one' and 'two'
for i = 1:number of structure files I have (the .mat files)
get the field names
for j = 1:number of field names
'this would be the name of the first field (so 'one')' = this would create a vector that concatenates all .one fields;
'this would be the name of the second field (so 'two')' = this would create a vector that concatenates all .two fields;
To try to be clearer, if = [1,2] and = [3,4], the result I'm hoping for is one = [1,2,3,4].
Stephen23 on 26 May 2020
There are two possible situations:
1- you know all of the fieldnames in advance, in which case it is simple to explicitly allocate them to variables:
one =;
two = S.two;
2- you don't know the fieldnames, in which case see previous comment. Note that having an unknown number of variables with unknown names in the workspace makes them difficult to process, increases the risk of bugs (e.g. overwriting a variable without any warning), and makes debugging difficult.
Personally I would just leave them in the structure: that little bit of extra typing makes the data easy to track and the code easy to maintain.

Sign in to comment.

Accepted Answer

Stephen23 on 26 May 2020
Edited: Stephen23 on 26 May 2020
Because all of the structures contain the same fieldnames they should be stored as one non-scalar structure:
>> S(1).one = [1,2];
>> S(1).two = NaN;
>> S(2).one = [3,4];
>> S(2).two = Inf;
Then the concatenation can be done in just a few lines:
>> C = struct2cell(S);
>> C = cellfun(@(c)[c{:}],num2cell(C,2:3),'uni',0);
>> Z = cell2struct(C,fieldnames(S),1);
ans =
1 2 3 4
>> Z.two
ans =
NaN Inf
EDIT: to load multiple mat files, where each contains one structure of unknown name:
for k = 1:numel(...)
F = ... filename
T = struct2cell(load(F));
S(k) = T{1};
Stephen23 on 27 May 2020
Edited: Stephen23 on 27 May 2020
"I think I may have misled you .... but the structures are all named the same"
That is okay, the main thing is, that we get there in the end :)
"Since I'm looping through them, if one of them is different, the code will err"
Not with the code that you have shown here, it would just continue and use the previous loop iteration's data. But you can easily write it in a more robust way (by load-ing into an output variable, e.g. T):
D = 'absolute/relative path to where the files are saved';
S = dir(fullfile(D,'*.mat'));
for k = 1:numel(S)
T = load(S(k).name);
S(k).one = T.structurename.fieldone;
Note that if the filenames contain numbers then your code could easily load them (and concatenate them) in an unexpected order. You should also read about array preallocation:

Sign in to comment.

More Answers (0)


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!