1

I have a structure array in which every value is a number, I would like to concatenate these structures into a single one.

Example:

S is structure array and every element has the same structure

S(1).a = 1
S(1).b.c = 1
S(1).b.d = 2

S(2).a = 2
S(2).b.c = 3
S(2).b.d = 4

cat(S) should be a structure 'CAT' with fields :

CAT.a = [1 2]
CAT.b.c = [1 3]
CAT.b.d = [2 4]
Hugo Trentesaux
  • 1,584
  • 1
  • 16
  • 30

1 Answers1

0

I did not find any matlab function to achieve this, so I programmed this function:

function out = catStruct(in)
% cat structure field per field

    if isstruct(in)
        for f = fields(in)'
            out.(f{:}) = catStruct([in.(f{:})]);
        end
    else
        out = in;
    end
end

If I do CAT = catStruct(S), I get what I want.

Hugo Trentesaux
  • 1,584
  • 1
  • 16
  • 30