首页 > 解决方案 > matlab中结构内的结构

问题描述

s(1) = struct('a', 2, 'b', 3)
s(1).c(1) = struct('x',4, 'y',5);
s(1).c(2) = struct('x',6, 'y',7);
s(1).c(3) = struct('x',8, 'y',9);

尝试此操作时出现错误

s(2) = struct('a', 10, 'b', 11)

Subscripted assignment between dissimilar structures.

我正在寻找一种方法来创建c(1),当我创建时直接创建子字段c(2),有没有办法做到这一点?c(3)s(1)

标签: matlabdata-structuresstruct

解决方案


c(1)您可以使用、c(2)c(3)如下方式创建您的结构,

s(1) = struct('a', 2, 'b', 3, 'c', [struct('x', 4, 'y', 5) struct('x', 6, 'y', 7) struct('x', 8, 'y', 9)])

如果您创建s(2)的字段需要与s(1). 但是,如果需要,您可以将其c留空。s(2)

s(2) = struct('a', 10, 'b', 11, 'c', struct)

推荐阅读