我有一个格式为:的结构
my_struct
|
+ element_1
| |
| + value_1: "some string"
| + value_2: 25
|
+ element_2
| |
| + value_1: "some other string"
| + value_2: 11
...
并且找不到一种简单的方法来创建结构数组,例如CCD_。类似地,my_struct(2).value_2 == 11
。字段名称";元素_ 1";以及";元素2";是不必要的。
以下是一种方法(请参见struct2cell
和cell2mat
(:
result = cell2mat(struct2cell(my_struct).');
示例:
my_struct.element_1.value1 = "some string";
my_struct.element_1.value2 = 25;
my_struct.element_2.value1 = "some other string";
my_struct.element_2.value2 = 11;
result = cell2mat(struct2cell(my_struct).');
给出
>> result
result =
1×2 struct array with fields:
value1
value2
>> result(1)
ans =
struct with fields:
value1: "some string"
value2: 25
>> result(2)
ans =
struct with fields:
value1: "some other string"
value2: 11