4.3 数组构架
4.3.1 构架数组的创建和显示
4.3.1.1 直接创建法及显示
【 * 例 4.3.1 .1-1 】本例通过温室数据(包括温室名、容积、温度、湿度等)演示:单构架的创建和显示。
(1)直接对域赋值法产生“单构架”,即
构架数组。
green_house.name=' 一号房 '; % 构架的域由(构架名) . (域名)标识。 <1>
green_house.volume='2000 立方米 '; %<2>
green_house.parameter.temperature=[31.2 30.4 31.6 28.7
29.7 31.1 30.9 29.6];%<3>
green_house.parameter.humidity=[62.1 59.5 57.7 61.5
62.0 61.9 59.2 57.5]; %<4>
(2)显示“单构架”结构和内容
green_house % 显示单构架结构 <5>
green_house =
name: ' 一号房 '
volume: '2000 立方米 '
parameter: [1x1 struct]
green_house.parameter % 显示 parameter 域中内容 <6>
ans =
temperature: [2x4 double]
humidity: [2x4 double]
green_house.parameter.temperature % 显示 temperature 域中的内容 <7>
ans =
31.2000 30.4000 31.6000 28.7000
29.7000 31.1000 30.9000 29.6000
【 * 例 4.3.1 .1-2 】本例演示构架数组的创建和显示,并利用构架数组保存一个温室群的数据。本例的运行以例 4.3.1.1-1 为先导。
(1)直接对域赋值法“构架数组”。
green_house(2,3).name=' 六号房 '; % 产生
构架数组 <1>
(2)显示构架数组的结构和构架元素的内容
green_house % 显示构架数组的结构:构架行列数;构架的域。 <2>
green_house =
2x3 struct array with fields:
name
volume
parameter
green_house(2,3) % 显示元素构架的结构:域;
否有子域 <3>
ans =
name: ' 六号房 '
volume: []
parameter: []
4.3.1.2 利用构造函数创建构架数组
【 * 例 4.3.1 .2-1 】利用构造函数 struct ,建立温室群的数据库。
(1) struct 预建空构架数组方法之一
a=cell(2,3); % 创建
空元胞数组
green_house_1=struct('name',a,'volume',a,'parameter',a(1,2)) % <2>
green_house_1 =
2x3 struct array with fields:
name
volume
parameter
(2) struct 预建空构架数组方法之二
green_house_2=struct('name',a,'volume',[],'parameter',[]) % <3>
green_house_2 =
2x3 struct array with fields:
name
volume
parameter
(3) struct 预建空构架数组方法之三
green_hopuse_3(2,3)=struct('name',[],'volume',[],'parameter',[])%<4>
green_hopuse_3 =
2x3 struct array with fields:
name
volume
parameter
(4) struct 创建构架数组方法之四
a1={' 六号房 '};a2={'3200 立方米 '};
green_house_4(2,3)=struct('name',a1,'volume',a2,'parameter',[]);%<6>
T6=[31.2,30.4,31.6,28.7;29.7,31.1,30.9,29.6]; % <7>
green_house_4(2,3).parameter.temperature=T6; % <8>
green_house_4
ans =
2x3 struct array with fields:
name
volume
parameter
4.3.2 构架数组域中内容的调取和设置
【 * 例 4.3.2 -1 】本例目的:一,演示函数 fieldnames , getfield , setfield 的使用方法;二,让读者感受到构架数组对应用工具包的影响;三,演示 struct 函数把“对象”转换为构架的应用。本例为获得一个演练的构架,借助 Toolbox control 工具包中的 tf 函数,先产生一个用传递函数描写的 LTI 线性时不变 2 输入 2 输出系统
。
(1)产生 2 输入 2 输出系统的传递函数阵“对象”
Stf=tf({3,2;[4 1],1},{[1 3 2],[1 1 1];[1 2 2 1],[1 0]})
Transfer function from input 1 to output...
3
#1: -------------
s^2 + 3 s + 2
4 s + 1
#2: ---------------------
s^3 + 2 s^2 + 2 s + 1
Transfer function from input 2 to output...
2
#1: -----------
s^2 + s + 1
1
#2: -
s
(2)为本例演示,把上述的 LTI 对象 Stf 转换为构架
SSTF=struct(Stf) % 把对象转换成构架,并显示构架的组成
SSTF =
num: {2x2 cell}
den: {2x2 cell}
Variable: 's'
lti: [1x1 lti]
(3)获得构架数组 SSTF 的域名
FN=fieldnames(SSTF) % 获得域名元胞数组 FN
class(FN) % 检查 FN 的类别
FN =
'num'
'den'
'Variable'
'lti'
ans =
cell
(4)获取 SSTF.den(2,1) 域的内容
FC=getfield(SSTF,'den',{2,1}) % 相当于 FC=SSFT.den(2,1)
FC{1} % 与 celldisp(FC) 的作用大致相当
poly2str(FC{1},'s'), % 为了把多项式显示成习惯的形式
FC =
[1x4 double]
ans =
1 2 2 1
ans =
s^3 + 2 s^2 + 2 s + 1
(5)重新设置 SSTF.num(2,1) 域的内容
SSTF.num{2,1} % 显示原始情况
SSTF=setfield(SSTF,'num',{2,1},{[1 3 1]}); % 注意“花括号”的使用
SSTF.num{2,1} % 显示被重新设置后的情况
ans =
0 0 4 1
ans =
1 3 1
4.3.3 构架数组操作深入
4.3.3.1 构架数组的扩充和收缩
【 * 例 4.3.3 .1-1 】本例演示构架数组 SSTF 的扩充和收缩。(本例以例 4.3.2-1 的运行为基础。)
(1)原构架是一个“单构架”
size(SSTF)
ans =
1 1
(2)演示构架的扩充
SSTF(2,2)=struct(tf(1,[1 1])) % 把 1/(s+1) 放在第 2 行第 2 列构架中
size(SSTF)
SSTF =
2x2 struct array with fields:
num
den
Variable
lti
ans =
2 2
(3)演示构架数组的收缩:删除构架数组的第 1 行
SSTF(1,:)=[] % 收缩成为
的构架
S22n=SSTF(1,2).num,S22d=SSTF(1,2).den % 取出第 2 构架 num 域和 den 域的内容
printsys(S22n{1},S22d{1}) % 显示成习惯的表达形式
SSTF =
1x2 struct array with fields:
num
den
Variable
lti
S22n =
[1x2 double]
S22d =
[1x2 double]
num/den =
1
-----
s + 1
4.3.3.2 增添域和删除域
【 * 例 4.3.3 .2-1 】对构架数组进行域的增添和删减操作。
(1)创建构架数组
clear,for k=1:10;department(k).number=['No.',int2str(k)];end
department
department =
1x10 struct array with fields:
number
(2)增添域:在数组中任何一个构架上进行的域增添操作,其影响遍及整个构架数组。
department(1).teacher=40;department(1).student=300;
department(1).PC_computer=40;
department
department =
1x10 struct array with fields:
number
teacher
student
PC_computer
(3)增添子域的操作只影响被操作的那个具体构架,而不是影响整个构架数组。
department(2).teacher.male=35;department(2).teacher.female=13;
D2T=department(2).teacher % 第 2 构架 teacher 域包含两个子域
D1T=department(1).teacher % 第 1 构架 teacher 域仅是一个数
D2T =
male: 35
female: 13
D1T =
40
(4)删除子域的操作也只影响被操作的那个具体构架。
department(2).teacher=rmfield(department(2).teacher,'male');
department(2).teacher
ans =
female: 13
(5)删除域的操作是对整个构架数组实施的。
department=rmfield(department,'student') % 删除一个域
department =
1x10 struct array with fields:
number
teacher
PC_computer
department=rmfield(department,{'teacher';'PC_computer'})% 删除 2 个域
department =
1x10 struct array with fields:
number
4.3.3.3 数值运算操作和函数对构架数组的应用
【 * 例 4.3.3 .3-1 】数值运算操作和函数在构架域上的作用。
n_ex=5; % 构架数组的
度
for k=1:n_ex, ex(k).f=(k-1)*n_ex+[1:5];end % 创建
构架数组
ex % 显示构架数组的结构
ex =
1x5 struct array with fields:
f
% 显示构架数组的域中内容
disp([blanks(10) ' 构架域中内容 '])
for k=1:n_ex,disp(ex(k).f),end
构架域中内容
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
class(ex(1).f) % 检查域中内容的类型
ans =
double
% 对各构架域中数值数组相应位置的数据相加求和
sum_f=zeros(1,5)
for k=1:n_ex,sum_f=sum_f+ex(k).f;end,sum_f
sum_f =
55 60 65 70 75
% 对构架数组域中各元素分别求平方根
disp([blanks(20) 'ex.f 的平方根值 '])
for k=1:n_ex, disp(sqrt(ex(k).f)),end
ex.f 的平方根值
1.0000 1.4142 1.7321 2.0000 2.2361
2.4495 2.6458 2.8284 3.0000 3.1623
3.3166 3.4641 3.6056 3.7417 3.8730
4.0000 4.1231 4.2426 4.3589 4.4721
4.5826 4.6904 4.7958 4.8990 5.0000
4.3.4 构架数组和元胞数组之间的转换
【 * 例 4.3.4 -1 】 指令 struct2cell 和 cell2struct 的使用。
(1)创建“带 2 个域的
构架数组”
for k=1:5,ex(k).s=['No.' int2str(k)];ex(k).f=(k-1)*5+[1:5];end
(2)显示构架数组的内容
fprintf('%s\n','ex.s 域的内容 ');fprintf('%s\',blanks(4))
for k=1:5;fprintf('%s\\',[ex(k).s blanks(1)]);end
fprintf('%s\n',blanks(1)),fprintf('%s\n','ex.f 域的内容 ')
for k=1:5;disp(ex(k).f);end % 显示 ex.f 域内容
ex.s 域的内容
No.1 \No.2 \No.3 \No.4 \No.5 \
ex.f 域的内容
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
(3)把 ex 构架数组转换为元胞数组
C_ex=struct2cell(ex);% “带 2 个域的
构架数组”转换为
元胞数组
size(C_ex)
fprintf('%s\',[C_ex{1,1,1},blanks(3)]) % 显示 C_ex 第 1 页第 1 行第 1 列内容
fprintf('% 5g \',C_ex{2,1,1}) % 显示 C_ex 第 2 页第 1 行第 1 列内容
ans =
2 1 5
No.1 1 2 3 4 5
(4)把元胞数组转换为构架数组之一
FS={'S_char';'F_num'}; % 用元胞数组预建域名字符串
EX1=cell2struct(C_ex,FS,1) % 元胞数组向构架数组转换
EX1 =
1x5 struct array with fields:
S_char
F_numric
EX1(1) % 观察新构架 EX1 第一构架的情况
ans =
S_char: 'No.1'
F_numric: [1 2 3 4 5]
(5)把元胞数组转换为构架数组之二
EX2=cell2struct(C_ex,'xx',2)
EX2 =
2x5 struct array with fields:
xx
(6)把元胞数组转换为构架数组之三
YY=strvcat('y1','y2','y3','y4','y5');EX3=cell2struct(C_ex,YY,3)
EX3 =
2x1 struct array with fields:
y1
y2
y3
y4
y5
EX3(1) % 观察第一构架情况
ans =
y1: 'No.1'
y2: 'No.2'
y3: 'No.3'
y4: 'No.4'
y5: 'No.5'
EX3(2) % 观察第二构架情况
ans =
y1: [1 2 3 4 5]
y2: [6 7 8 9 10]
y3: [11 12 13 14 15]
y4: [16 17 18 19 20]
y5: [21 22 23 24 25]
【 * 例 4.3.4 -2 】带子域的构架数组转换为元胞数组。本例中的 ex 构架数组由例 4.3.4-1 生成,然后再运行以下程序。
ex(1,1).s % 原构架 ex(1,1).s 中的内容
ans = No.1
% 增设子域,并把 ex 构架数组扩充为
。
ex(1,1).s.sub='SUB 1'; % 原 ex(1,1).s 中的字符串将因本指令而消失。
ex(3,1).s.sub='SUB 3';
ex(3,1).s.num=1/3;
ex(1,1).s % 经新赋值后, ex(1,1).s 中的内容
ans =
sub: 'SUB 1'
ex(3,1).s % 经新赋值后, ex(3,1).s 中的内容
ans =
sub: 'SUB 3'
num: 0.3333
C_ex_sub=struct2cell(ex) % 把构架转换为元胞数组
C_ex_sub(:,:,1) =
[1x1 struct] [] [1x1 struct]
[1x5 double] [] []
C_ex_sub(:,:,2) =
'No.2' [] []
[1x5 double] [] []
C_ex_sub(:,:,3) =
'No.3' [] []
[1x5 double] [] []
C_ex_sub(:,:,4) =
'No.4' [] []
[1x5 double] [] []
C_ex_sub(:,:,5) =
'No.5' [] []
[1x5 double] [] []
size(C_ex_sub) % 观察新元胞数组的大小
ans =
2 3 5
C_ex_sub{1,1,1} % 观察第一元胞中的内容
ans =
sub: 'SUB 1'
C_ex_sub{1,3,1} % 观察 (1,3,1) 元胞中的内容
ans =
sub: 'SUB 3'
num: 0.3333

激光扫描服务技术具有很高的经济意...

(资料图片)“中国载人航天飞行任务试验用车”、“航天员凯旋礼宾车”、“神舟七号飞...

针对级进模排样的特点以及人工智能技术在工程领域应用的研究,提出了适用于级进模排样...

目前,北京现代汽车有限公司发动机厂拥有两个工厂,分别于2004年和2007年投产,年生产...

9月8日,石家庄兆通金刚石工贸有限公司销售经理王金龙先生在向笔者介绍金刚石制品行业...