Matlab实训3-字符串元胞和结构数组.ppt

上传人:小飞机 文档编号:6511878 上传时间:2023-11-08 格式:PPT 页数:35 大小:239.99KB
返回 下载 相关 举报
Matlab实训3-字符串元胞和结构数组.ppt_第1页
第1页 / 共35页
Matlab实训3-字符串元胞和结构数组.ppt_第2页
第2页 / 共35页
Matlab实训3-字符串元胞和结构数组.ppt_第3页
第3页 / 共35页
Matlab实训3-字符串元胞和结构数组.ppt_第4页
第4页 / 共35页
Matlab实训3-字符串元胞和结构数组.ppt_第5页
第5页 / 共35页
点击查看更多>>
资源描述

《Matlab实训3-字符串元胞和结构数组.ppt》由会员分享,可在线阅读,更多相关《Matlab实训3-字符串元胞和结构数组.ppt(35页珍藏版)》请在三一办公上搜索。

1、实训二字符串数组、元胞数组和结构数组,2.1 字符串数组2.2 元胞数组(单元数组)2.3 结构数组(构架数组),2.1字符串数组,2.1.1字符串构造,t=How about this character string?t=How about this character string?size(t)ans=1 32 whos Name Size Bytes Class t 1x32 64 char arrayGrand total is 34 elements using 80 bytes,u=abs(t)u=Columns 1 through 12 72 111 119 32 97 98

2、 111 117 116 32 116 104 Columns 13 through 24 105 115 32 99 104 97 114 97 99 116 101 114 Columns 25 through 32 32 115 116 114 105 110 103 63 char(u)ans=How about this character string?,u=t(16:24)u=characteru=Hello,;v=World!;,v=Character strings having more than one row must have the same number of c

3、olumn just like matrices!v=Character strings having more thanone row must have the same number of column just like matrices!,w=u vw=Hello,World!disp(w)Hello,World!,lengends=char(Wilt,Russel,Kareem)lengends=Wilt RusselKareem,char(one,tow,three)ans=one tow three strvcat(one,two,three)ans=one two three

4、,2.1.2数字与字符串的相互转换,rad=2.5;area=pi*rad2;t=A circle of radius num2str(rad)has an area of num2str(area).;disp(t)A circle of radius 2.5 has an area of 19.63,t=sprintf(A circle of radius%.4g has an area of%.4g.,rad,area);disp(t)A circle of radius 2.5 has an area of 19.63.fprintf(A circle of radius%.4g ha

5、s an area of%.4g.n,rad,area)A circle of radius 2.5 has an area of 19.63.,fprintf与sprintf的区别是,前者把转换结果书写于屏幕或指定的文件,而后者则是把转换结果存放于变量,2.1.3字符串函数,a=eval(sqrt(2)a=1.4142 eval(a=sqrt(2)a=1.4142,a=feval(sqrt,2)a=1.4142,b=Peter Piper picked a peck of pickled peppers;findstr(b,)%find spaceans=6 12 19 21 26 29 3

6、7findstr(b,p)%find the letter pans=9 13 22 30 38 40 41find(b=p)%for single character searches ans=9 13 22 30 38 40 41findstr(b,cow)%find the word cowans=findstr(b,pick)%find the string pickans=13 30,strrep(b,p,P)%capitalize all p sans=Peter PiPer Picked a Peck of Pickled PePPersstrrep(b,Peter,Pamela

7、)%change Peter to Pamelaans=Pamela Piper picked a peck of pickled peppers,disp(b)Peter Piper picked a peck of pickled peppersstrtok(b)%ans=Peterc,r=strtok(b)c=Peterr=Piper picked a peck of pickled peppers,2.2单元数组,2.2.1单元数组的创建,A(1,1)=1 2 3;4 5 6;7 8 9;A(1,2)=2+3i;A(2,1)=A character atring;A(2,2)=12:-

8、2:0;AA=3x3 double 2.0000+3.0000iA character atring 1x7 double,A1,1=1 2 3;4 5 6;7 8 9;A1,2=2+3i;A2,1=A character string;A2,2=12:-2:0;AA=3x3 double 2.0000+3.0000iA character string 1x7 double,单元索引,按值寻址,A(1,1)ans=3x3 double,A1,1ans=1 2 3 4 5 6 7 8 9,celldisp(A)A1,1=1 2 3 4 5 6 7 8 9 A2,1=A character at

9、ring A1,2=2.0000+3.0000iA2,2=12 10 8 6 4 2 0cellplot(A,legend),B=1 2,John Smith,;2+3i,5B=1x2 double John Smith2.0000+3.0000i 5,C=cell(2,3)C=,C(1,1)=This doesnt work?Conversion to cell from char is not possible.,C(1,1)=This does workC=This does work C2,3=This works tooC=This does work This works too,

10、2.2.2单元数组处理,AA=3x3 double 2.0000+3.0000i A character string 1x7 double BB=1x2 double John Smith 2.0000+3.0000i 5 C=A;BC=3x3 double 2.0000+3.0000i A character string 1x7 double 1x2 double John Smith 2.0000+3.0000i 5,D=C(1 3,:)D=3x3 double 2.0000+3.0000i1x2 double John Smith C(3,:)=C=3x3 double 2.0000

11、+3.0000i A character string 1x7 double 2.0000+3.0000i 5,2.2.3获得单元数组的内容,BB=1x2 double John Smith 2.0000+3.0000i 5 x=B2,2x=5 class(x)ans=double,y=B(2,2)y=5 y=B(4)y=5 class(y)ans=cell class(y1)ans=double,d,e=deal(B:,2)d=John Smithe=5,B:,2ans=John Smithans=5 d=B:,2?Illegal right hand side in assignment.

12、Too many elements.,celldisp(A)A1,1=1 2 3 4 5 6 7 8 9A2,1=A character string A1,2=2.0000+3.0000iA2,2=12 10 8 6 4 2 0,A1,1(3,:)ans=7 8 9 A4(2:5)ans=10 8 6 4 A2,1(3:11)ans=character,2.3结构数组,2.3.1创建结构数组,circle.radius=2.5;circle.center=0,1;circle.linestyle=-;circle.color=red;circlecircle=radius:2.5000 ce

13、nter:0 1 linestyle:-color:red,circle(2).radius=3.4;circle(2).color=green;circle(2).linestyle=:;circle(2).center=2.3-1.2;circlecircle=1x2 struct array with fields:radius center linestylecolor,circle(2).radius=sqrt(2);circlecircle=1x2 struct array with fields:radius center linestylecolor circle.radius

14、ans=2.5000ans=sqrt(2),circle(1).filled=yescircle=1x3 struct array with fields:radius center linestyle colorfilled circle.filledans=yesans=ans=,circle(2).filled=no;circle(3).filled=yes;circle.filledans=yesans=noans=yes,values1=2.5 sqrt(2),25.4;values2=0 1 2.3-1.2-1 0;values3=-,:,-.;values4=red,green,

15、blue;values5=yes,no,yes;CIRCLE=struct(radius,values1,center,values2,.)linestyle,values3,color,values4,filled,values5)CIRCLE=1x3 struct array with fields:radius center linestyle colorfilled,2.3.2结构处理,A=circle CIRCLEA=1x6 struct array with fields:radius center linestyle color filled,square.width=5;squ

16、are.height=14;square.center=zeros(1,2);square.rotation=pi/4;B=circle square?Error using=horzcatCAT arguments are not consistent in structure field number.,2.3.3获取结构内容,circlecircle=1x3 struct array with fields:radius center linestyle colorfilled rad2=circle(2).radiusrad2=sqrt(2)circle(1).radiusans=2.

17、5000,col=circle.color?Illegal right hand side in assignment.Too many elements.,c1,c2,c3=deal(circle.color)c1=redc2=greenc3=blue,4.3.4结构函数,circle=1x3 struct array with fields:radius center linestyle color filled,fieldnames(circle)ans=radius center linestyle color filled,isfield(circle,color)ans=1 isf

18、ield(circle,width)ans=0,class(square)ans=struct isstruct(circle)ans=1 d=pi;isstruct(d)ans=0,fnames=fieldnames(circle)fnames=radius center linestyle color filled,circle2=rmfield(circle,fnames5)circle2=1x3 struct array with fields:radius center linestyle color circle3=rmfield(circle,radius)circle3=1x3 struct array with fields:center linestyle color filled,rad1=getfield(circle,1,fnames1)rad1=2.5000 rad3=getfield(circle,3,fnames1)rad3=25.4000,circle4=setfield(circle,3,fnames1,8)circle4=1x3 struct array with fields:radius center linestyle color filled,要点,字符串数组的创建字符串转换和操作函数单元数组的创建和内容获取结构数组的创建和内容获取,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号