MATLAB字符串数组

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第三章MATLAB字符串数组、元

胞数组和构架数组

3.1字符串数组

3.1.1字符串入门

【例3.1-1】先请读者实际操作本例,以体会数值量与字符串的区别。

clear

a=12345.6789

class(a)

a_s=size(a)

a =

1.2346e+004

ans =

double

a_s =

1 1

b='S'

class(b)

b_s=size(b)

b =

S

ans =

char

b_s =

1 1

whos

Name Size Bytes Class

a 1x1 8 double array

a_s 1x2 16 double array

ans 1x4 8 char array

b 1x1 2 char array

b_s 1x2 16 double array

Grand total is 10 elements using 50 bytes

3.1.2串数组的属性和标识

【例3.1-2】本例演示:串的基本属性、标识和简单操作。

a='This is an example.'

a =

This is an example.

size(a)

ans =

1 19

a14=a(1:4)

ra=a(end:-1:1)

a14 =

This

ra =

.elpmaxe na si sihT

ascii_a=double(a)

ascii_a =

Columns 1 through 12

84 104 105 115 32 105 115 32 97 110 32 101 Columns 13 through 19

120 97 109 112 108 101 46

char(ascii_a)

ans =

This is an example.

w=find(a>='a'&a<='z');

ascii_a(w)=ascii_a(w)-32;

char(ascii_a)

ans =

THIS IS AN EXAMPLE.

A='这是一个算例。';

A_s=size(A)

A56=A([5 6])

ASCII_A=double(A)

A_s =

1 7

A56 =

算例

ASCII_A =

Columns 1 through 6

54754 51911 53947 47350 52195 49405 Column 7

41379

char(ASCII_A)

ans =

这是一个算例。

b='Example ''3.1.2-1'''

b =

Example '3.1.2-1'

ab=[a(1:7),' ',b,' .']

ab =

This is Example '3.1.2-1' .

3.1.3复杂串数组的创建

一多行串数组的直接创建

【例3.1-3】多行串数组的直接输入示例。

clear

S=['This string array '

'has multiple rows.']

S =

This string array

has multiple rows.

size(S)

ans =

2 18

二利用串操作函数创建多行串数组

【例3.1-4】演示:用专门函数char , str2mat , strvcat创建多行串数组示例。

S1=char('This string array','has two rows.')

S1 =

This string array

has two rows.

S2=str2mat('这','字符','串数组','由4行组成')

S2 =

字符

串数组

由4行组成

S3=strvcat('这','字符','串数组',' ','由4行组成')

S3 =

字符

串数组

由4行组成

size(S3)

ans =

5 5

三转换函数产生数码字符串

【例3.1-5】最常用的数组/字符串转换函数int2str , num2str , mat2str 示例。

A=eye(2,4);

A_str1=int2str(A)

A_str1 =

1 0 0 0

0 1 0 0

rand('state',0)

B=rand(2,4);

B3=num2str(B,3)

B3 =

0.95 0.607 0.891 0.456

0.231 0.486 0.762 0.0185

B_str=mat2str(B,4)

B_str =

[0.9501 0.6068 0.8913 0.4565;0.2311 0.486 0.7621 0.0185]

Expression=['exp(-',B_str,')'];

eval(Expression)

ans =

0.3867 0.5451 0.4101 0.6335

0.7937 0.6151 0.4667 0.9817

【例3.1-6】综合例题:在MATLAB计算生成的图形上标出图名和最大值点坐标。(见图3.1-1) clear

相关文档
最新文档