如何向结构体数组添加新元素?我无法连接空结构:
>> a=struct;
>> a.f1='hi'
a =
f1: 'hi'
>> a.f2='bye'
a =
f1: 'hi'
f2: 'bye'
>> a=cat(1,a,struct)
Error using cat
Number of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of
fields.
那么是否可以添加带有空字段的新元素?
UPDATE
我发现如果我同时添加新字段,我可以添加新元素:
>> a=struct()
a =
struct with no fields.
>> a.f1='hi';
>> a.f2='bye';
>> a(end+1).iamexist=true
a =
1x2 struct array with fields:
f1
f2
iamexist
令人难以置信的是,没有直路!结构中可能有一些冒号等效项吗?
如果您懒得再次输入字段或者字段太多,那么这里有一个获取空字段结构的捷径
a.f1='hi'
a.f2='bye'
%assuming there is not yet a variable called EmptyStruct
EmptyStruct(2) = a;
EmptyStruct = EmptyStruct(1);
now EmptyStruct
是您想要的空结构。所以要添加新的
a(2) = EmptyStruct; %or cat(1, a, EmptyStruct) or [a, EmptyStruct] etc...
a(2)
ans =
f1: []
f2: []
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)