
本文为matlab自学笔记的一部分,之所以学习matlab是因为其真的是人工智能无论是神经网络还是智能计算中日常使用的,非常重要的软件。也许最近其带来的一些负面消息对国内各个高校和业界影响很大。但是我们作为技术人员,更是要奋发努力,拼搏上进,学好技术,才能师夷长技以制夷,为中华之崛起而读书!
本文很多摘录自图书资料,不做任何商业用途,仅做技术分享,侵权删除!请不要放弃自己的理想和道路,加油!!
“参考文献 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.html
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
bcombined(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
blarger(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
titlefields = 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)'}创建一个包含多个字段的结构体。
S1 = struct('b',1,'B',2,'a',3,'A',4)
S1 = struct with fields:
b: 1
B: 2
a: 3
A: 4
对字段排序。此语法基于 ASCII 顺序按字段名称对字段排序。
S = orderfields(S1)
S = struct with fields:
A: 4
B: 2
a: 3
b: 1创建两个结构体,它们具有相同字段,只是字段顺序不同。字段名称相同,但字段值不同。
S1 = struct('b',1,'B',2,'a',3,'A',4)
S1 = struct with fields:
b: 1
B: 2
a: 3
A: 4
S2 = struct('a',0,'b',20,'B',10,'A',0)
S2 = struct with fields:
a: 0
b: 20
B: 10
A: 0
对 S1 中的字段进行排序以匹配 S2 中的字段顺序。
S = orderfields(S1,S2)
S = struct with fields:
a: 3
b: 1
B: 2
A: 4创建一个结构体。
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]创建一个结构体。
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]