如何创建具有变量名的结构

How to create struct with variable name

我想创建一个以动态变量命名的结构。 类似于:

for t = 1:2

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

当查看 Father 时,有 Father.1.1.AFather.1.2.A、...、Father.2.70.C

感谢您的帮助。

您可以使用以下示例创建 struct(如提到的 excaza,不允许以数字开头的字段名称):

A = 1;
C = 3;

for b = 1:7
    Father.(['b', num2str(b)]) = struct('A', A, 'C', C);
end

现在:

Father.b1.A 等于 1
Father.b5.C 等于 3

MATLAB 允许结构数组可以像其他数组一样进行索引:

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