matlab中使用结构体 -- 结构(struct)数组 【转】

标签:
杂谈 |
分类: MatlabStudy |
要在MALTAB中实现比较复杂的编程,就不能不用struct类型。而且在Matlab中实现struct比C中更为方便。 4. 3.1 结构数组的创建 Matlab提供了两种定义结构的方式:直接应用和使用struct函数。 1. 使用直接引用方式定义结构 与建立数值型数组一样,建立新struct对象不需要事先申明,可以直接引用,而且可以动态扩充。比如建立一个复数变量x: x.real = 0; % 创建字段名为real,并为该字段赋值为0 x.imag = 0 % 为x创建一个新的字段imag,并为该字段赋值为0 x = real: 0 imag: 0 然后可以将其动态扩充为数组: x(2).real = 0; % 将x扩充为1×2的结构数组 x(2).imag = 0; 在任何需要的时候,也可以为数组动态扩充字段,如增加字段scale: x(1).scale = 0; 这样,所有x都增加了一个scale字段,而x(1)之外的其他变量的scale字段为空: x(1) % 查看结构数组的第一个元素的各个字段的内容 ans = real: 0 imag: 0 scale: 0 x(2) % 查看结构数组的第二个元素的各个字段的内容,注意没有赋值的字段为空 ans = real: 0 imag: 0 scale: [] 应该注意的是,x的real、imag、scale字段不一定是单个数据元素,它们可以是任意数据类型,可以是向量、数组、矩阵甚至是其他结构变量或元胞数组,而且不同字段之间其数据类型不需要相同。例如: clear x; x.real = [1 2 3 4 5]; x.imag = ones(10,10); 数组中不同元素的同一字段的数据类型也不要求一样: x(2).real = '123'; x(2).imag = rand(5,1); 甚至还可以通过引用数组字段来定义结构数据类型的某字段: x(3).real = x(1); x(3).imag = 3; x(3) ans = real: [1x1 struct] imag: 3 下面看一个实际的例子来熟悉直接引用方式定义与显示结构。 【例4.3.1-1】 温室数据(包括温室名、容量、温度、湿度等)的创建与显示。 (1) 直接对域赋值法产生结构变量 green_house.name = '一号温室'; % 创建温室名字段 green_house.volume = '2000立方米'; % 创建温室容量字段 green_house.parameter.temperature = [31.2 30.4 31.6 28.7 % 创建温室温度字段 29.7 31.1 30.9 29.6]; green_house.parameter.humidity = [62.1 59.5 57.7 61.5; % 创建温室湿度字段 62.0 61.9 59.2 57.5]; (2)显示结构变量的内容 green_house % 显示结构变量结构 green_house = name: '一号温室' volume: '2000立方米' parameter: [1x1 struct] green_house.parameter % 用域作用符号. 显示指定域(parameter)中内容 ans = temperature: [2x4 double] humidity: [2x4 double] green_house.parameter.temperature % 显示temperature域中的内容 ans = 31.2000 30.4000 31.6000 28.7000 29.7000 31.1000 30.9000 29.6000 【例4.3.1-2】在上例的基础上,创建结构数组用以保存一个温室群的数据。 green_house(2,3).name = '六号温室'; %产生2×3结构数组 green_house % 显示结构数组的结构 green_house = 2x3 struct array with fields: name volume parameter green_house(2,3) % 显示结构数组元素的结构 ans = name: '六号温室' volume: [] parameter: [] 2. 使用struct函数创建结构 使用struct函数也可以创建结构,该函数产生或吧其他形式的数据转换为结构数组。 struct的使用格式为: s = sturct('field1',values1,'field2',values2,…); 该函数将生成一个具有指定字段名和相应数据的结构数组,其包含的数据values1、valuese2等必须为具有相同维数的数据,数据的存放位置域其他结构位置一一对应的。对于struct的赋值用到了元胞数组。数组values1、values2等可以是元胞数组、标量元胞单元或者单个数值。每个values的数据被赋值给相应的field字段。 当valuesx为元胞数组的时候,生成的结构数组的维数与元胞数组的维数相同。而在数据中不包含元胞的时候,得到的结构数组的维数是1×1的。例如: s = struct('type',{'big','little'},'color',{'blue','red'},'x',{3,4}) s = 1x2 struct array with fields: type color x 得到维数为1×2的结构数组s,包含了type、color和x共3个字段。这是因为在struct函数中{'big','little'}、{'blue','red'}和{3,4}都是1×2的元胞数组,可以看到两个数据成分分别为: s(1,1) ans = type: 'big' color: 'blue' x: 3 ans = type: 'little' color: 'red' x: 4 相应的,如果将struct函数写成下面的形式: s = struct('type',{'big';'little'},'color',{'blue';'red'},'x',{3;4}) s = 2x1 struct array with fields: type color x 则会得到一个2×1的结构数组。 下面给出利用struct构建结构数组的具体实例。 【例4.3.1-3】利用函数struct,建立温室群的数据库。 (1) struct预建立空结构数组方法之一 a = cell(2,3); % 创建2×3的元胞数组 green_house_1=struct('name',a,'volume',a,'parameter',a(1,2)) green_house_1 = 2x3 struct array with fields: name volume parameter (2)struct预建空结构数组方法之二 green_house_2=struct('name',a,'volume',[],'parameter',[]) green_house_2 = 2x3 struct array with fields: name volume parameter (3)struct预建空结构数组方法之三 green_hopuse_3(2,3)=struct('name',[],'volume',[],'parameter',[]) 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',[]); T6=[31.2,30.4,31.6,28.7;29.7,31.1,30.9,29.6]; green_house_4 ans = 2x3 struct array with fields: name volume parameter 4. 3.2 结构数组的操作 MATLAB中专门用于对结构数组的操作的函数并不多,通过 help datatypes获取数据类型列表,可以看到其中的结构数据类型的有关的函数,主要如表4.3.1所示。 表4.3.1 结构数组的操作函数 函数名 deal getfield setfield struct2cell isstruct 下面举一些具体的例子说明如果对结构数组加以操作。 【例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 = 'num' 'den' 'Variable' 'lti' ans = cell (4)获取SSTF.den(2,1)域的内容 FC=getfield(SSTF,'den',{2,1}) FC{1} 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.2-2】本例演示结构数组SSTF的扩充和收缩。(本例以例4.3.2-1的运行为基础。) (1)原结构是一个“单结构” size(SSTF) ans = 1 1 (2)演示结构的扩充 SSTF(2,2)=struct(tf(1,[1 1])) size(SSTF) SSTF = 2x2 struct array with fields: num den Variable lti ans = 2 2 (3)演示结构数组的收缩:删除结构数组的第1行 SSTF(1,http://bbs.matwav.com/images/smiles/smile.gif-- S22n=SSTF(1,2).num,S22d=SSTF(1,2).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.2-3】对结构数组进行域的增添和删减操作。 (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 D1T=department(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.2-4】数值运算操作和函数在结构域上的作用。 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.2-5】 (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 |