资源预览内容
第1页 / 共35页
第2页 / 共35页
第3页 / 共35页
第4页 / 共35页
第5页 / 共35页
第6页 / 共35页
第7页 / 共35页
第8页 / 共35页
第9页 / 共35页
第10页 / 共35页
亲,该文档总共35页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
实训二 字符串数组、元胞数组和结构数组,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 array Grand total is 34 elements using 80 bytes, u=abs(t) u = Columns 1 through 12 72 111 119 32 97 98 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 = character u= Hello, ; v= World! ;,v= Character strings having more than one row must have the same number of column just like matrices! v = Character strings having more than one row must have the same number of column just like matrices!,w=u v w = Hello,World! disp(w) Hello,World!, lengends=char(Wilt,Russel,Kareem) lengends = Wilt Russel Kareem, char(one,tow,three) ans = one tow three strvcat(one,two,three) ans = one two three,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 has 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 space ans = 6 12 19 21 26 29 37 findstr(b, p ) % find the letter p ans = 9 13 22 30 38 40 41 find (b= = p ) % for single character searches ans = 9 13 22 30 38 40 41 findstr(b, cow ) % find the word cow ans = findstr(b, pick ) % find the string pick ans = 13 30, strrep(b, p , P ) % capitalize all p s ans = Peter PiPer Picked a Peck of Pickled PePPers strrep(b, Peter , Pamela ) % change Peter to Pamela ans = Pamela Piper picked a peck of pickled peppers,disp(b) Peter Piper picked a peck of pickled peppers strtok(b) % ans = Peter c, r=strtok(b) c = Peter r = 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:-2:0; A A = 3x3 double 2.0000+ 3.0000i A 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; A A = 3x3 double 2.0000+ 3.0000i A character string 1x7 double,单元索引,按值寻址, A(1,1) ans = 3x3 double, A1,1 ans = 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 atring A1,2 = 2.0000 + 3.0000i A2,2 = 12 10 8 6 4 2 0 cellplot(A,legend), B=1 2,John Smith,;2+3i,5 B = 1x2 double John Smith 2.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 work C = This does work C2,3=This works too C = This does work This works too,2.2.2 单元数组处理, A A = 3x3 double 2.0000+ 3.0000i A character string 1x7 double B B = 1x2 double John Smith 2.0000+ 3.0000i 5 C=A;B C = 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.0000i 1x2 double John Smith C(3,:)= C = 3x3 double 2.0000+ 3.0000i A character string 1x7 double 2.0000+ 3.0000i 5,2.2.3 获得单元数组的内容, B B = 1x2 double John Smith 2.0000+ 3.0000i 5 x=B2,2 x = 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 Smith e = 5, B:,2 ans = John Smith ans = 5 d=B:,2 ? Illegal right hand side in assignment. Too many elements., celldisp(A) A1,1 = 1 2 3 4 5 6 7 8 9 A2,1 = A character string A1,2 = 2.0000 + 3.0000i A2,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.line-; circle.color=red; circle circle = radius: 2.5000 center: 0 1 linestyle: - color: red, circle(2).radius=3.4; circle(2).color=green; circle(2).line:; circle(2).center=2.3 -1.2; circle circle = 1x2 struct array with fields: radius center linestyle color, circle(2).radius=sqrt(2); circle circle = 1x2 struct array with fields: radius center linestyle color circle.radius ans = 2.5000 ans = sqrt(2), circle(1).filled=yes circle = 1x3 struct array with fields: ra
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号