第 4 章字符串数组、元胞数组和结构数组

36
第 4 第第第第第第 第第第第第第第第第 4.1 字字字字字 4.2 字字字字 字字字字字 () 4.3 字字字字 字字字字字 ()

Upload: jorden-alvarado

Post on 14-Mar-2016

75 views

Category:

Documents


4 download

DESCRIPTION

第 4 章字符串数组、元胞数组和结构数组. 4.1 字符串数组 4.2 元胞数组(单元数组) 4.3 结构数组(构架数组). 4.1 字符串数组. 4.1.1 字符串构造. >> t='How about this character string?' t = How about this character string? >> size(t) ans = 1 32 >> whos Name Size Bytes Class - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 第 4 章字符串数组、元胞数组和结构数组

1

第 4 章字符串数组、元胞数组和结构数组4.1 字符串数组4.2 元胞数组(单元数组)4.3 结构数组(构架数组)

Page 2: 第 4 章字符串数组、元胞数组和结构数组

2

4.1字符串数组 4.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

Page 3: 第 4 章字符串数组、元胞数组和结构数组

3

>> 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?

Page 4: 第 4 章字符串数组、元胞数组和结构数组

4

>>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 thanone row must have the same number of column just like matrices!

>>w=[u v]w =

Hello,World!>>disp(w)

Hello,World!

Page 5: 第 4 章字符串数组、元胞数组和结构数组

5

>> lengends=char(‘Wilt’,‘Russel’,‘Kareem)lengends =Wilt RusselKareem

>> char('one','','tow','three')ans =one tow three>> strvcat('one','','two','three')ans =one two three

Page 6: 第 4 章字符串数组、元胞数组和结构数组

6

4.1.2 数字与字符串的相互转换Dec2he

x十进制数到十六进制字符串转换

fprintf 把格式化的文本写到文件中或显示屏上hex2de

c十六进制字符串转换成十进制数

hex2num

十六进制字符串转换成 IEEE浮点数int2str 整数转换成字符串lower 字符串转换成小写num2st

r数字转换成字符串

setstr ASCII转换成字符串sprintf 用格式控制,数字转换成字符串sscanf 用格式控制,字符串转换成数字str2mat 字符串转换成一个文本矩阵str2nu

m字符串转换成数字

upper 字符串转换成大写

Page 7: 第 4 章字符串数组、元胞数组和结构数组

7

>>rad=2.5; area=pi*rad^2;>>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.

Page 8: 第 4 章字符串数组、元胞数组和结构数组

8

fprintf(' %.0e\n ',pi) 3e+00fprintf(' %.3e\n ',pi) 3.142e+00fprintf(' %.10e\n ',pi) 3.1415926536e+00fprintf(' %.0f\n ',pi) 3fprintf(' %.3f\n ',pi) 3.142fprintf(' %.10f\n ',pi) 3.1415926536fprintf(' %.0g\n ',pi) 3fprintf(' %.3g\n ',pi) 3.14fprintf(' %.10g\n ',pi) 3.141592654fprintf(' %.8.0g\n ',pi) 3fprintf(' %.8.3g\n ',pi) 3.14fprintf(' %.8.10g\n ',pi) 3.141592654

Page 9: 第 4 章字符串数组、元胞数组和结构数组

9

4.1.3 字符串函数eval(string) 作为一个MATLAB命令求字符串的值blanks(n) 返回一个 n个零或空格的字符串deblank 去掉字符串中后拖的空格feval 求由字符串给定的函数值findstr 从一个字符串内找出字符串isletter 字母存在时返回真值isspace 空格字符存在时返回真值isstr 输入是一个字符串,返回真值lasterr 返回上一个所产生MATLAB错误的字符串strcmp 字符串相同,返回真值strrep 用一个字符串替换另一个字符串strtok 在一个字符串里找出第一个标记

Page 10: 第 4 章字符串数组、元胞数组和结构数组

10

>>a=eval(' sqrt(2) ')a =

1.4142>> eval(' a=sqrt(2) ')a =

1.4142

>>a=feval(' sqrt ' ,2)a =

1.4142

Page 11: 第 4 章字符串数组、元胞数组和结构数组

11

>>b='Peter Piper picked a peck of pickled peppers ' ;>>findstr(b,' ') % find spaceans =

6 12 19 21 26 29 37>>findstr(b,' p ') % find the letter pans =

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 cowans =

[ ]>> findstr(b,' pick ') % find the string pickans =

13 30

Page 12: 第 4 章字符串数组、元胞数组和结构数组

12

>> strrep(b, ' p ', ' P ') % capitalize all p ' sans =

Peter PiPer Picked a Peck of Pickled PePPers>>strrep(b, ' Peter ', ' Pamela ') % change Peter to Pamelaans =

Pamela Piper picked a peck of pickled peppers

Page 13: 第 4 章字符串数组、元胞数组和结构数组

13

>>disp(b)Peter Piper picked a peck of pickled peppers

>>strtok(b) % ans =

Peter>>[c, r]=strtok(b) c =

Peterr =

Piper picked a peck of pickled peppers>>strtok(b,'a')ans =Peter Piper picked

Page 14: 第 4 章字符串数组、元胞数组和结构数组

14

4.2单元数组

Page 15: 第 4 章字符串数组、元胞数组和结构数组

15

4.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};>>AA = [3x3 double] [2.0000+ 3.0000i]'A character atring' [1x7 double]

>> A{1,1}=[1 2 3;4 5 6;7 8 9];>> A{1,2}=2+3i;>> A{2,1}='A character string';>> A{2,2}=12:-2:0;>>AA = [3x3 double] [2.0000+ 3.0000i]'A character string' [1x7 double]

单元索引

按值寻址

>> A(1,1)ans = [3x3 double]

>> A{1,1}ans = 1 2 3 4 5 6 7 8 9

Page 16: 第 4 章字符串数组、元胞数组和结构数组

16

>> celldisp(A) A{1,1} = 1 2 3 4 5 6 7 8 9 A{2,1} = A character atring A{1,2} = 2.0000 + 3.0000iA{2,2} = 12 10 8 6 4 2 0

>>cellplot(A,'legend')

Page 17: 第 4 章字符串数组、元胞数组和结构数组

17

Page 18: 第 4 章字符串数组、元胞数组和结构数组

18

>> 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 doesn''t work'??? Conversion to cell from char is not possible.

>> C(1,1)={'This does work'}C = 'This does work' [] [] [] [] []>> C{2,3}='This works too'C = 'This does work' [] [] [] [] 'This works too'

Page 19: 第 4 章字符串数组、元胞数组和结构数组

19

4.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;B]C = [3x3 double] [2.0000+ 3.0000i] 'A character string' [1x7 double] [1x2 double] 'John Smith' [ 2.0000+ 3.0000i] [ 5]

Page 20: 第 4 章字符串数组、元胞数组和结构数组

20

>> 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]

Page 21: 第 4 章字符串数组、元胞数组和结构数组

21

4.2.3 获得单元数组的内容>> BB = [1x2 double] 'John Smith' [2.0000+ 3.0000i] [ 5]>> x=B{2,2}x = 5>> class(x)ans =

double

>> y=B(2,2)y = [5]>> y=B(4)y = [5]>> class(y)ans =

cell>> class(y{1})ans =

double

Page 22: 第 4 章字符串数组、元胞数组和结构数组

22

>> [d,e]=deal(B{:,2})d =John Smithe = 5

>> B{:,2}ans =John Smithans = 5>> d=B{:,2}??? Illegal right hand side in assignment. Too many

elements.

Page 23: 第 4 章字符串数组、元胞数组和结构数组

23

>> celldisp(A) A{1,1} = 1 2 3 4 5 6 7 8 9A{2,1} = A character string A{1,2} = 2.0000 + 3.0000iA{2,2} = 12 10 8 6 4 2 0

>> A{1,1}(3,:)ans = 7 8 9>> A{4}(2:5)ans = 10 8 6 4>> A{2,1}(3:11)ans =character

Page 24: 第 4 章字符串数组、元胞数组和结构数组

24

4.3结构数组 4.3.1 创建结构数组

>> circle.radius=2.5;>> circle.center=[0,1];>> circle.linestyle='--';>> circle.color='red';>> circlecircle = radius: 2.5000 center: [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

Page 25: 第 4 章字符串数组、元胞数组和结构数组

25

>> circle(2).radius='sqrt(2)';>> circlecircle = 1x2 struct array with fields: radius center linestylecolor>> circle.radiusans = 2.5000ans =sqrt(2)

Page 26: 第 4 章字符串数组、元胞数组和结构数组

26

>> Cradius=[2.5 3.4];>> Ccenter=[0 1;2.3 -1.2];>> Clinestyle={'--' ':'};>> Ccolor={'red','green'};

>> circle(3).radius=25.4;>> circle(3).center=[ -1 0];>> circle(3).linestyle='-.';>> circle(3).color='blue';

>> Cradius(3)=25.4;>> Ccenter(3,:)=[-1 0];>> Clinestyle{3}='-.';>> Ccolor{3}='blue'

myfunc(circle)

myfunc(Cradius,Ccenter,Clinestyle,Ccolor)

Page 27: 第 4 章字符串数组、元胞数组和结构数组

27

>> circle(1).filled='yes'circle = 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 =ye

Page 28: 第 4 章字符串数组、元胞数组和结构数组

28

>> values1={2.5 'sqrt(2)',25.4};>> values2={[0 1] [2.3 -1.2] [-1 0]};>> values3={'--',':','-.'};>> values4={'red','green','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

Page 29: 第 4 章字符串数组、元胞数组和结构数组

29

4.3.2 结构处理>> A=[circle CIRCLE]A = 1x6 struct array with fields: radius center linestyle color filled

>> square.width=5;>> square.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.

Page 30: 第 4 章字符串数组、元胞数组和结构数组

30

4.3.3 获取结构内容>>circlecircle = 1x3 struct array with

fields: radius center linestyle colorfilled>> rad2=circle(2).radiusrad2 =sqrt(2)>> circle(1).radiusans =2.5000

Page 31: 第 4 章字符串数组、元胞数组和结构数组

31

>> col=circle.color??? Illegal right hand side in assignment. Too many elements.

>>[c1,c2,c3]=deal(circle.color)c1 =redc2 =greenc3 =blue

Page 32: 第 4 章字符串数组、元胞数组和结构数组

32

4.3.4 结构函数>>circle =

1x3 struct array with fields: radius center linestyle color filled

>> fieldnames(circle)

ans =

'radius' 'center' 'linestyle' 'color' 'filled'

Page 33: 第 4 章字符串数组、元胞数组和结构数组

33

>> isfield(circle,'color')ans = 1>> isfield(circle,'width')ans = 0

>> class(square)ans =struct>> isstruct(circle)ans = 1>> d=pi;>> isstruct(d)ans = 0

Page 34: 第 4 章字符串数组、元胞数组和结构数组

34

>> fnames=fieldnames(circle)fnames = 'radius' 'center' 'linestyle' 'color

>> circle2=rmfield(circle,fnames{5})circle2 = 1x3 struct array with fields: radius center linestyle color>> circle3=rmfield(circle,'radius')circle3 = 1x3 struct array with fields: center linestyle color filled

Page 35: 第 4 章字符串数组、元胞数组和结构数组

35

>> rad1=getfield(circle,{1},fnames{1})rad1 = 2.5000>> rad3=getfield(circle,{3},fnames{1})rad3 = 25.4000

>> circle4=setfield(circle,{3},fnames{1},8)circle4 = 1x3 struct array with fields: radius center linestyle color filled

Page 36: 第 4 章字符串数组、元胞数组和结构数组

36

要点 字符串数组的创建 字符串转换和操作函数 单元数组的创建和内容获取 结构数组的创建和内容获取