参考文献
https://ww2.mathworks.cn/help/matlab/ref/rmfield.html
https://ww2.mathworks.cn/help/matlab/ref/arrayfun.html
https://ww2.mathworks.cn/help/matlab/ref/structfun.html
https://ww2.mathworks.cn/help/matlab/ref/struct2cell.html
https://ww2.mathworks.cn/help/matlab/ref/cell2struct.htmlhtml
定义一个包含 first、second、third 和 fourth 字段的标量结构体。 S.first = 1; S.second = 2; S.third = 3; S.fourth = 4; 删除字段 first 和 fourth。 fields = {'first','fourth'}; S = rmfield(S,fields) S = struct with fields: second: 2 third: 3
将函数应用于每一个数组元素,区别在于structfun 的输入参数必须是标量结构体。git
建立一个非标量结构体数组。每一个结构体有一个包含随机数向量的字段。这些向量具备不一样的大小。 S(1).f1 = rand(1,5); S(2).f1 = rand(1,10); S(3).f1 = rand(1,15) S = 1x3 struct array with fields: f1 使用 arrayfun 函数计算 S 中每一个字段的均值。不能使用 structfun 完成此计算,由于 structfun 的输入参数必须是标量结构体。 A = arrayfun(@(x) mean(x.f1),S) A = 1×3 0.6786 0.6216 0.6069
建立一个结构体数组,其中每一个结构体有两个包含数值数组的字段。 S(1).X = 5:5:100; S(1).Y = rand(1,20); S(2).X = 10:10:100; S(2).Y = rand(1,10); S(3).X = 20:20:100; S(3).Y = rand(1,5) S = 1x3 struct array with fields: X Y 绘制数值数组。从 plot 函数返回一个图形线条对象的数组,并使用这些对象为每一组数据点添加不一样的标记。arrayfun 能够返回任何数据类型的数组,只要该数据类型的对象能够串联便可。 figure hold on p = arrayfun(@(a) plot(a.X,a.Y),S); p(1).Marker = 'o'; p(2).Marker = '+'; p(3).Marker = 's'; hold off
S(1).f1 = rand(3,5);
S(2).f1 = rand(6,10);
S(3).f1 = rand(4,2)
S = 1x3 struct array with fields:
f1github
使用 arrayfun 函数计算 S 中每一个字段的均值。mean 返回包含每列均值的向量,所以不能以数组的形式返回均值。要以元胞数组的形式返回均值,请指定 'UniformOutput',false 名称-值对组。数组
A = arrayfun(@(x) mean(x.f1),S,'UniformOutput',false)
A = 1x3 cell array
{1x5 double} {1x10 double} {1x2 double}
```函数
建立一个非标量结构体数组。 S(1).f1 = 1:10; S(2).f1 = [2; 4; 6]; S(3).f1 = [] S = 1x3 struct array with fields: f1 使用 arrayfun 函数计算 S 中每一个字段的大小。行数和列数分别输出在两个 1×3 数值数组中。 [nrows,ncols] = arrayfun(@(x) size(x.f1),S) nrows = 1×3 1 3 0 ncols = 1×3 10 1 0
对标量结构体的每一个字段应用函数--和arrayfun不一样,arrayfun对全部元素应用函数,structfun对全部字段应用函数学习
S.f1 = 1:10;
S.f2 = [2; 4; 6];
S.f3 = []
S = struct with fields:
f1: [1 2 3 4 5 6 7 8 9 10]
f2: [3x1 double]
f3: []spa
计算每一个数值数组的均值,而后以数组的形式返回这些均值。3d
A = structfun(@mean,S)
A = 3×1code
5.5000
4.0000
NaNorm
* A = structfun(func,S,Name,Value) * A = structfun(func,S,Name,Value) 应用 func 并使用一个或多个 Name,Value 对组参数指定其余选项。例如,要以结构体形式返回输出值,**请指定 'UniformOutput',false。** 当 func 返回的值不能合并为数组时,能够按结构体形式返回 A。返回的结构体具备与 S 相同的字段。
建立一个标量结构体,其字段中包含矩阵。
S.f1 = 1:10;
S.f2 = [2 3; 4 5; 6 7];
S.f3 = rand(4,4)
S = struct with fields:
f1: [1 2 3 4 5 6 7 8 9 10]
f2: [3x2 double]
f3: [4x4 double]
计算每一个矩阵的均值。mean 返回包含每列均值的向量,所以不能以数组的形式返回均值。要以结构体形式返回均值,请指定 'UniformOutput',false 名称-值对组。
A = structfun(@mean,S,'UniformOutput',false)
A = struct with fields:
f1: 5.5000
f2: [4 5]
f3: [0.6902 0.3888 0.7627 0.5962]
* [A1,...,Am] = structfun( ___ ) * 当 func 返回 m 个输出值时,[A1,...,Am] = structfun(_ __ ) 返回多个输出数组 A1,...,Am。func 能够返回不一样数据类型的输出参数,但每次调用 func 时返回的每一个输出的数据类型必须相同。能够将此语法与前面语法中的任何输入参数结合使用。
建立一个标量结构体。
S.f1 = 1:10;
S.f2 = [2 3; 4 5; 6 7];
S.f3 = rand(4,4)
S = struct with fields:
f1: [1 2 3 4 5 6 7 8 9 10]
f2: [3x2 double]
f3: [4x4 double]
计算 S 中每一个数组的大小。行数和列数都是一个 3×1 数值数组。
[nrows,ncols] = structfun(@size,S)
nrows = 3×1
1 3 4
ncols = 3×1
10 2 4
```
建立一个结构体。 S.x = linspace(0,2*pi); S.y = sin(S.x); S.title = 'y = sin(x)' S = struct with fields: x: [1x100 double] y: [1x100 double] title: 'y = sin(x)' 将 S 转换为元胞数组。 C = struct2cell(S) C = 3x1 cell array {1x100 double} {1x100 double} {'y = sin(x)'} 元胞数组不包含字段名称。要返回元胞数组中的字段名称,请使用 fieldnames 函数。fieldnames 和 struct2cell 以相同的顺序返回字段名称和值。 fields = fieldnames(S) fields = 3x1 cell array {'x' } {'y' } {'title'}
输入如下命令以建立初始元胞数组 employees: devel = {{'Lee','Reed','Hill'}, {'Dean','Frye'}, ... {'Lane','Fox','King'}}; sales = {{'Howe','Burns'}, {'Kirby','Ford'}, {'Hall'}}; mgmt = {{'Price'}, {'Clark','Shea'}, {'Sims'}}; qual = {{'Bates','Gray'}, {'Nash'}, {'Kay','Chase'}}; docu = {{'Lloyd','Young'}, {'Ryan','Hart','Roy'}, {'Marsh'}}; employees = [devel; sales; mgmt; qual; docu] employees = {1x3 cell} {1x2 cell} {1x3 cell} {1x2 cell} {1x2 cell} {1x1 cell} {1x1 cell} {1x2 cell} {1x1 cell} {1x2 cell} {1x1 cell} {1x2 cell} {1x2 cell} {1x3 cell} {1x1 cell}
rowHeadings = {'development', 'sales', 'management', 'quality', 'documentation'};
depts = cell2struct(employees, rowHeadings, 1) depts = 3x1 struct array with fields: development sales management quality documentation
depts(1:2).development ans = 'Lee' 'Reed' 'Hill' ans = 'Dean' 'Frye'
colHeadings = {'fiveYears' 'tenYears' 'fifteenYears'}; years = cell2struct(employees, colHeadings, 2) years = 5x1 struct array with fields: fiveYears tenYears fifteenYears
[~, sales_5years, ~, ~, docu_5years] = years.fiveYears sales_5years = 'Howe' 'Burns' docu_5years = 'Lloyd' 'Young'
rowHeadings = {'development', 'documentation'}; depts = cell2struct(employees([1,5],:), rowHeadings, 1) depts = 3x1 struct array with fields: development documentation
for k=1:3 depts(k,:) end ans = development: {'Lee' 'Reed' 'Hill'} documentation: {'Lloyd' 'Young'} ans = development: {'Dean' 'Frye'} documentation: {'Ryan' 'Hart' 'Roy'} ans = development: {'Lane' 'Fox' 'King'} documentation: {'Marsh'}