参考文献
https://ww2.mathworks.cn/help/matlab/matlab_prog/concatenate-structures.html
https://ww2.mathworks.cn/help/matlab/ref/struct.html#d117e1320181
https://ww2.mathworks.cn/help/matlab/ref/fieldnames.html
https://ww2.mathworks.cn/help/matlab/ref/isfield.html
https://ww2.mathworks.cn/help/matlab/ref/isstruct.html
https://ww2.mathworks.cn/help/matlab/ref/orderfields.htmlhtml
struct1.a = 'first'; struct1.b = [1,2,3]; struct2.a = 'second'; struct2.b = rand(5); struct1,struct2 struct1 = struct with fields: a: 'first' b: [1 2 3] struct2 = struct with fields: a: 'second' b: [5x5 double]
combined = [struct1,struct2] combined = 1x2 struct array with fields: a b
combined(1).a ans = 'first'
new(1,1).a = 1; new(1,1).b = 10; new(1,2).a = 2; new(1,2).b = 20; new(2,1).a = 3; new(2,1).b = 30; new(2,2).a = 4; new(2,2).b = 40; larger = [combined; new] larger = 3x2 struct array with fields: a b
larger(2,1).a ans = 1
建立包含多个字段的非标量结构体。 field1 = 'f1'; value1 = zeros(1,10); field2 = 'f2'; value2 = {'a', 'b'}; field3 = 'f3'; value3 = {pi, pi.^2}; field4 = 'f4'; value4 = {'fourth'}; s = struct(field1,value1,field2,value2,field3,value3,field4,value4) s = 1x2 struct array with fields: f1 f2 f3 f4 value2 和 value3 的元胞数组是 1×2 数组,所以 s 也是 1×2 数组。由于 value1 是数值数组而不是元胞数组,因此 s(1).f1 和 s(2).f1 具备相同的内容。相似地,由于 value4 的元胞数组具备单一元素,因此 s(1).f4 和 s(2).f4 具备相同的内容。 s(1) ans = struct with fields: f1: [0 0 0 0 0 0 0 0 0 0] f2: 'a' f3: 3.1416 f4: 'fourth' s(2) ans = struct with fields: f1: [0 0 0 0 0 0 0 0 0 0] f2: 'b' f3: 9.8696 f4: 'fourth'
建立一个结构体数组。 S(1,1).x = linspace(0,2*pi); S(1,1).y = sin(S(1,1).x); S(1,1).title = 'y = sin(x)'; S(2,1).x = linspace(0,2*pi); S(2,1).y = cos(S(2,1).x); S(2,1).title = 'y = cos(x)' S = 2x1 struct array with fields: x y title
fields = fieldnames(S) fields = 3x1 cell array {'x' } {'y' } {'title'}
values = struct2cell(S) values = 3x2 cell array {1x100 double} {1x100 double} {1x100 double} {1x100 double} {'y = sin(x)'} {'y = cos(x)'}
结构体数组的顺序字段git
S1 = struct('b',1,'B',2,'a',3,'A',4)
S1 = struct with fields:
b: 1
B: 2
a: 3
A: 4github
对字段排序。此语法基于 ASCII 顺序按字段名称对字段排序。数组
S = orderfields(S1)
S = struct with fields:
A: 4
B: 2
a: 3
b: 1函数
* S = orderfields(S1,S2) * S = orderfields(S1,S2) 返回 S1 的副本,其字段已从新排序以匹配 S2 的字段顺序。输入结构体数组 S1 和 S2 必须具备相同的字段名称。
建立两个结构体,它们具备相同字段,只是字段顺序不一样。字段名称相同,但字段值不一样。学习
S1 = struct('b',1,'B',2,'a',3,'A',4)
S1 = struct with fields:
b: 1
B: 2
a: 3
A: 4spa
S2 = struct('a',0,'b',20,'B',10,'A',0)
S2 = struct with fields:
a: 0
b: 20
B: 10
A: 0code
对 S1 中的字段进行排序以匹配 S2 中的字段顺序。htm
S = orderfields(S1,S2)
S = struct with fields:
a: 3
b: 1
B: 2
A: 4排序
* S = orderfields(S1,C) * S = orderfields(S1,C) 按输入数组 C 匹配名称顺序。S1 中每一个字段的名称必须在 C 中出现一次。
建立一个结构体。
data.x = linspace(0,2*pi);
data.y = sin(data.x);
data.title = 'y = sin(x)'
data = struct with fields:
x: [1x100 double]
y: [1x100 double]
title: 'y = sin(x)'
经过以元胞数组形式列出字段名称来对字段排序。
C = {'title','x','y'};
data = orderfields(data,C)
data = struct with fields:
title: 'y = sin(x)'
x: [1x100 double]
y: [1x100 double]
* S = orderfields(S1,P) * S = orderfields(S1,P) 按置换向量 P 匹配顺序。 若是 S1 有 n 个字段,则 P 的元素是从 1 到 n 的整数,按任意顺序排列。例如,若是 S1 有三个字段,P 是 [3 1 2],则 S1 的第三个字段是输出 S 的第一个字段。当须要以相同的方式对多个结构体数组进行排序时,此语法很是有用。
建立一个结构体。
data.x = linspace(0,2*pi);
data.y = sin(data.x);
data.title = 'y = sin(x)'
data = struct with fields:
x: [1x100 double]
y: [1x100 double]
title: 'y = sin(x)'
经过以另外一顺序列出字段的原始位置来对字段排序。例如,移动第三个字段,使其成为输出结构体的第一个字段。
P = [3 1 2];
data = orderfields(data,P)
data = struct with fields:
title: 'y = sin(x)'
x: [1x100 double]
y: [1x100 double]
```