森之张卫东 发表于 2015-8-10 20:59

结构数组的操作函数 2——例题(二)

【例1-2】本例演示结构数组SSTF的扩充和收缩。(本例以例1-1的运行为基础。)(1)原结构是一个“单结构”size(SSTF) ans =1 1 (2)演示结构的扩充SSTF(2,2)=struct( tf(1,) )     % 把1/(s+1)放在第2行第2列结构中size(SSTF) SSTF = 2x2 struct array with fields:numdenVariableltians =2 2 (3)演示结构数组的收缩:删除结构数组的第1行SSTF( 1,Smile=[]                   % 收缩成为 的结构S22n=SSTF(1,2).num,S22d=SSTF(1,2).den   % 取出第2结构num域和den域的内容printsys(S22n{1}, S22d{1} )           % 显示成习惯的表达形式 SSTF = 1x2 struct array with fields:numdenVariableltiS22n = S22d = num/den = 1-----s + 1 【例1-3】对结构数组进行域的增添和删减操作。(1)创建结构数组clear,fork=1:10;department(k).number=['No.',int2str(k)];enddepartment 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:numberteacherstudentPC_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: 35female: 13D1T =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:numberteacherPC_computer department=rmfield(department,{'teacher';'PC_computer'})%删除2个域department = 1x10 struct array with fields:number

页: [1]
查看完整版本: 结构数组的操作函数 2——例题(二)