Search code examples
matlabmatlab-struct

How to create struct with variable name


I would like to create a struct that is named after a dynamic variable. Something like:

for t = 1:2

    for b = 1:70
        Father.t.b.A = A;
        Father.t.b.C = C;
    end
end

and when looking at Father there is Father.1.1.A, Father.1.2.A, ... , Father.2.70.C.

Thanks for any help.


Solution

  • MATLAB allows for arrays of structures that can be indexed similarly to its other arrays:

    for t = 1:2
        for b = 1:70
            Father(t, b).A = A;
            Father(t, b).C = C;
        end
    end