How to combine 2 structures into 1?

3 次查看(过去 30 天)
John
John 2020-3-13
>> a = struct('a1',1,'a2',2,'a3',3)
a =
struct with fields:
a1: 1
a2: 2
a3: 3
>> b=struct('b1',11,'b2',12,'b3',13,'b4',14)
b =
struct with fields:
b1: 11
b2: 12
b3: 13
b4: 14
Want to combine structures a and b into c like:
>> c = struct('a1',1,'a2',2,'a3',3,'b1',11,'b2',12,'b3',13,'b4',14)
c =
struct with fields:
a1: 1
a2: 2
a3: 3
b1: 11
b2: 12
b3: 13
b4: 14

回答(1 个)

Fangjun Jiang
Fangjun Jiang 2020-3-13
d=[struct2cell(a);struct2cell(b)];
f=[fieldnames(a);fieldnames(b)];
c=cell2struct(d,f)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品


版本

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by