MatlabR2009a版第3讲单元数组和结构.ppt_第1页
MatlabR2009a版第3讲单元数组和结构.ppt_第2页
MatlabR2009a版第3讲单元数组和结构.ppt_第3页
MatlabR2009a版第3讲单元数组和结构.ppt_第4页
MatlabR2009a版第3讲单元数组和结构.ppt_第5页
已阅读5页,还剩47页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

MATLAB2009 从入门到精通 Date1 课程主要内容 第1章 MATLAB简介 第2章 数值运算 第3章 单元数组和结构 第4章 字符串 第5章 符号运算 第6章 MATLAB绘图基础 第7章 程序设计 第8章 计算方法的MATLAB实现 第9章 优化设计 第10章 SIMULINK仿真初探 Date2 第3章 单元数组和结构 在前面章节已经介绍了几种MATLAB2009常用的数 据类型。本章将介绍两种特殊的数据类型,即单元 数组和结构体数组,这两种数据类型的特点是允许 用户将不同但是相关的数据类型集成到一个单一的 变量。这样,因为相关的数据可以通过一个单元数 组或是结构体的数组进行组织和访问,数据的管理 就变得相对要容易一些。 Date3 3.1 单元数组 单元数组就是每一个元素为一个单元的数组。每 个单元可以包含任意数据类型的MATLAB2009数 组。例如,单元数组的一个单元可以是一个实数 矩阵,或是一个字符串数组,也可以是一个复向 量数组。 Date4 3.1.1 单元数组的生成 直接生成单元数组: 用类似矩阵的记号将给复杂的数据结构纳入一个 变量之下。和矩阵中的圆括号表示下标类似,单 元数组由大括号表示下标。包括左标志法和右标 志法。 左标志法:左标志法就是把单元标志放在左边 。 右标志法:右标志法就是把单元标志放在右边 。 Date5 左标志法 c1,1=abc; c1,2=eye(2); c2,1=1 2;3 4; c2,2=1 2; c c = abc 2x2 double 2x2 double 1x2 double Date6 右标志法 c(1,1)=abc; c(1,2)=eye(2); c(2,1)=1 2;3 4; c(2,2)=1 2; c c = abc 2x2 double 2x2 double 1x2 double Date7 上面的单元数组也可由下面的方法直接得到: c=abc,eye(2);1 2;3 4,1 2 c = abc 2x2 double 2x2 double 1x2 double Date8 使用cell函数生成单元数组: cell(n)生成一个n*n阶的置空单元数组; cell(m,n) 或cell(m,n)生成一个m*n阶的置空单元 数组; cell(m,n,p,)或cell(m,n,p,)生成一个m*n*p 阶的置空单元数组; cell(size(a)生成与a同形式的单元型置空矩阵。 Date9 cell函数生成单元数组 a=cell(2) a = Date10 a1,1=1 2;2 2 a = 2x2 double Date11 a1,2=MATLAB2009 a = 2x2 double MATLAB2009 Date12 a2,1=peking a = 2x2 double MATLAB2009 peking Date13 a2,2=1 2 a = 2x2 double MATLAB2009 peking 1x2 double Date14 3.1.2 单元数组显示 单元数组的显示有以下几种方式 一是直接在命令窗口中输入单元数组的名字; 二是使用函数celldisp(c)来输出。 想得到单元数组中某一个单元的值时,可以采用 c(m,n)或cm,n的格式输出。但是它们得到的结果 表示形式是不同的。 函数celldisp(c)更适用于具有大量数据的单元数组 的显示。 cm,n 和celldisp(c)显示的结果形式相同。 celldisp(a,name)在窗口中显示的单元型变量的内 容的名称为name。 Date15 程序实例 c=abc,eye(2);1 2;3 4,1 2; c c = abc 2x2 double 2x2 double 1x2 double Date16 celldisp(c) c1,1 = abc c2,1 = 1 2 3 4 c1,2 = 1 0 0 1 c2,2 = 1 2 Date17 c=abc,eye(2);1 2;3 4,1 2; c(1,2) ans = 2x2 double c1,2 ans = 1 0 0 1 Date18 a=1 2,1 2 3;2 3,2 3 4; celldisp(a,hh) hh1,1 = 1 2 hh2,1 = 2 3 hh1,2 = 1 2 3 hh2,2 = 2 3 4 Date19 3.1.3 单元数组绘图 h=cellplot(c)返回一个向量,这个向量综合体现了 表面、线和句柄。 h=cellplot(c,legend)返回一个向量,这个向量综 合体现了表面、线和句柄,并有图形注释。 Date20 程序实例 a=1 2;2 2,MATLAB2009;peking,1 2; h=cellplot(a) h = 152.0029 153.0024 154.0024 155.0024 156.0024 157.0024 158.0024 Date21 Date22 程序实例 a=1 2;2 2,MATLAB2009;peking,1 2; h=cellplot(a,legend) h = 152.0035 153.0026 154.0026 155.0026 156.0026 157.0026 158.0026 Date23 Date24 3.1.4 单元数组合并与变形 使用c=a,b或c=a;b将两个单元数组集成一个更 大的数组。 使用c(n,:)=删除单元数组的第n行。 reshape(x,m,n)和reshape(x,m,n)将单元数组x的所 有元素分配到一个m*n的单元数组,但单元数组 元素不是m*n时,返回错误信息。行列数不相等 的话,仍是按列优先的原则。 Date25 单元数组合并实例 a=1 2,as; b=2 5,AD; c=a,b c = 1x2 double as 1x2 double AD d=a;b d = 1x2 double as 1x2 double AD Date26 单元数组删除实例 d=1 2,as;2 5,AD; d(1,:)= d = 1x2 double AD Date27 单元数组变形实例 d=1 2,as;2 5,AD; c=reshape(d,1,4) c = 1x2 double 1x2 double as AD Date28 3.2 结构型变量 结构体是根据属性名组织起来的不同类型数据的 集合。结构体和单元数组的共同之处在于它们都 提供了一种分级存储机制来存储不同类型的数据 ,不同之处是组织数据的方式不一样。结构体数 组里的数据是通过属性名来引用的,而在单元数 组里,数据是通过单元数组下标引用来操作的。 Date29 3.2.1 结构型变量的生成 直接输入法: 采用直接输入法时,在给结构体成员元素直接赋 值的同时定义该元素的名称,并使用点儿将结构 型变量和成员元素名连接。 Date30 程序实例 student.test=99 56 96 87 67 69 87 76 92; =Huang Liang; student.weight=67; student.height=1.68; student.num=034093; student.add=School of civil engneering.Tsinghua university; student.tel Date31 结构体显示 输入结构体名称student实现结构体显示: student student = test: 99 56 96 87 67 69 87 76 92 name: Huang Liang weight: 67 height: 1.6800 num: 34093 add: School of civil engneering.Tsinghua university tel:Date32 增加结构体变量 可以通过以下形式语句添加新的结构变量(另外的 学生数据)。 student(2).test=99 65 88 78 76 98 75 96 59; %系统将默认之前的student为student(1) student(2).name=Wei Huan; student(2).weight=50; student(2).height=1.58; student(2).num=034999; student(2).num=034999; student(2).add=School of Psychology.Chongqing university; student(2).tel=02361701456; Date33 此时,输入student将只得到该结构的成员变量名 而不显示内容 student student = 1x2 struct array with fields: test name weight height num add tel Date34 可以继续在窗口中输入student(1)或student(2)来查 询其具体内容。 student(2) ans = test: 99 65 88 78 76 98 75 96 59 name: Wei Huan weight: 50 height: 1.5800 num: 34999 add: School of Psychology.Chongqing university tel: 02361701456 Date35 使用struct函数生成结构变量 Struct_array=struct(field,v1,field2,v2,),其中 ,field为各成员变量名,v1等为对应的各成员变 量的内容,但是必须配对出现。 Date36 程序实例 student=struct(test,99 65 88 78 76 98 75 96 59,name,Wei Huan,weight,50,height,1.5800) student = test: 99 65 88 78 76 98 75 96 59 name: Wei Huan weight:50 height:1.5800 Date37 失败的生成实例 student=struct(test,99 65 88 78 76 98 75 96 59,name,Wei Huan,weight,50,height) ? Error using = struct Field and value input arguments must come in pairs. Date38 添加结构体变量 student=struct(fen,86,gao,176) student = fen: 86 gao: 176 student(2)=struct(fen,90,gao,170) student = 1x2 struct array with fields: fen gao Date39 3.2.2 结构体变量的操作 在结构体变量中添加成员变量 student(1).gender=Male; student(1).age=25; student(2).gender=Female; student(2).age=21; Date40 在结构体变量中删除成员变量 s=rmfield(s,field)从m*n阶的结构体变量s中删除 指定的成员变量field。但该函数仍保留原有结构 形式。 s=rmfield(s,fields)当fields是单元型变量时,将一次 性删除多个成员变量。 Date41 程序实例 s=rmfield(student,name) s = test: 99 56 96 87 67 69 87 76 92 weight: 67 height: 1.6800 num: 34093 add: School of civil engneering.Tsinghua university tel:Date42 s=rmfield(student,weight,test) s = name: Huang Liang height: 1.6800 num: 34093 add: School of civil engneering.Tsinghua university tel:Date43 在结构体变量中调用成员变量 结构变量中的任何信息,可以通过“结构体名称+ 成员变量名”的格式进行取出。 例如:student(1).test 从结构体变量中获取某个成员变量时,可以直接 输入变量名+成员名称,也可以采用循环语句的方 式。 例如:student.test Date44 程序实例 ans = Huang Liang ans = Wei Huan for i=1:length(student) disp(student(i).name); end Huang Liang Wei Huan Date45 函数法取值和赋值 getfield函数取得当前存储在某个成员变量中的值 。表达式f=getfild(s,field)返回指定成员变量的内 容,与表达式f=s.field等价。 setfield函数给某个成员变量插入新的值。表达式 s=setfield(s,field,v)将成员变量field的值设置为v 与表达式s.field=v等价。 Date46 程序实例 c=getfield(student,name) c = Wei Huan c=setfield(student,name,Tom) c = test: 99 65 88 78 76 98 75 96 59 name: Tom weight: 50 height: 1.58

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论