我正在尝试在循环中分配结构的字段值。
具有空值的结构声明:
result_struct = struct('a',{},'b',{},'c',{},'d',{})
我在这样的循环中分配值:
% assume a, b, c, d are variables in my workspace
% field names match with the variable names
for index=1:n
% some computation and store results in variables (a-d)
result_struct(index).a = a;
result_struct(index).b = b;
result_struct(index).c = c;
result_struct(index).d = d;
end
如何使用另一个循环将值分配给字段?像那样:
for fname = fieldnames(result_struct)'
result_struct(index).fname = fname; % field names and variable names match
end