如果我有一个结构,句柄,
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
哪个返回:
handles =
a: 1
b: 2
c: 3
d: 4
e: 5
f: 6
关于string - 向现有结构添加更多字段名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30631702/