我有不同大小的数组,如[6:8]
、[11:21]
、[14:15]
等。我需要将这些值分配给一个变量集,该变量集应该包含在不同数组中指定的字符串。让我举一个例子:
variables = ["a", "b", "c"];
% I need to design a structure just like below but inside a for loop.
% Because I've lots of variables and arrays.
xx.a = [6:8];
xx.b = [11:21];
xx.c = [14:15];
有什么解决方案建议吗?
我不知道数据是如何组织的,但您可以使用variables
数组的字符串动态索引结构字段:
variables = ["a", "b", "c"];
data{1} = [6:8];
data{2} = [11:21];
data{3} = [14:15];
% Use string indexing of structs within loop
for ii = 1:numel(variables)
xx.(variables(ii)) = data{ii};
end
xx =
struct with fields:
a: [6 7 8]
b: [11 12 13 14 15 16 17 18 19 20 21]
c: [14 15]