向现有结构添加更多字段名

Add more fieldnames to an existing struct

如果我有一个结构,句柄,

handles = struct('a',1,'b',2,'c',3)

我还有一个字符串单元格和一个数字单元格

cell1 = {'d','e','f'};
cell2 = {4,5,6};

如何将单元格 1 中的字段名称添加到具有单元格 2 中的值的句柄中?

虽然可能有更有效的方法,但首先想到的是利用 dynamic field names:

handles = struct('a',1,'b',2,'c',3);

cell1 = {'d','e','f'};
cell2 = {4,5,6};

for ii = 1:length(cell1)
    handles.(cell1{ii}) = cell2{ii};
end  

哪个returns:

handles = 

    a: 1
    b: 2
    c: 3
    d: 4
    e: 5
    f: 6