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]

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.