MATLAB字符串数组.docx

上传人:b****5 文档编号:11952830 上传时间:2023-04-16 格式:DOCX 页数:23 大小:31.94KB
下载 相关 举报
MATLAB字符串数组.docx_第1页
第1页 / 共23页
MATLAB字符串数组.docx_第2页
第2页 / 共23页
MATLAB字符串数组.docx_第3页
第3页 / 共23页
MATLAB字符串数组.docx_第4页
第4页 / 共23页
MATLAB字符串数组.docx_第5页
第5页 / 共23页
点击查看更多>>
下载资源
资源描述

MATLAB字符串数组.docx

《MATLAB字符串数组.docx》由会员分享,可在线阅读,更多相关《MATLAB字符串数组.docx(23页珍藏版)》请在冰豆网上搜索。

MATLAB字符串数组.docx

MATLAB字符串数组

第三章MATLAB字符串数组、元胞数组和构架数组

.1字符串数组

.1.1字符串入门

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

clear

a=12345.6789

class(a)

a_s=size(a)

a=

1.2346e+004

ans=

double

a_s=

11

b='S'

class(b)

b_s=size(b)

b=

S

ans=

char

b_s=

11

whos

NameSizeBytesClass

a1x18doublearray

a_s1x216doublearray

ans1x48chararray

b1x12chararray

b_s1x216doublearray

Grandtotalis10elementsusing50bytes

.1.2串数组的属性和标识

【例3.1-2】本例演示:

串的基本属性、标识和简单操作。

a='Thisisanexample.'

a=

Thisisanexample.

size(a)

ans=

119

a14=a(1:

4)

ra=a(end:

-1:

1)

a14=

This

ra=

.elpmaxenasisihT

ascii_a=double(a)

ascii_a=

Columns1through12

8410410511532105115329711032101

Columns13through19

1209710911210810146

char(ascii_a)

ans=

Thisisanexample.

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

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

char(ascii_a)

ans=

THISISANEXAMPLE.

A='这是一个算例。

';

A_s=size(A)

A56=A([56])

ASCII_A=double(A)

A_s=

17

A56=

算例

ASCII_A=

Columns1through6

547545191153947473505219549405

Column7

41379

char(ASCII_A)

ans=

这是一个算例。

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

b=

Example'3.1.2-1'

ab=[a(1:

7),'',b,'.']

ab=

ThisisExample'3.1.2-1'.

.1.3复杂串数组的创建

10一多行串数组的直接创建

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

clear

S=['Thisstringarray'

'hasmultiplerows.']

S=

Thisstringarray

hasmultiplerows.

size(S)

ans=

218

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

【例3.1-4】演示:

用专门函数char,str2mat,strvcat创建多行串数组示例。

S1=char('Thisstringarray','hastworows.')

S1=

Thisstringarray

hastworows.

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

S2=

字符

串数组

由4行组成

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

S3=

字符

串数组

由4行组成

size(S3)

ans=

55

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

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

A=eye(2,4);

A_str1=int2str(A)

A_str1=

1000

0100

rand('state',0)

B=rand(2,4);

B3=num2str(B,3)

B3=

0.950.6070.8910.456

0.2310.4860.7620.0185

B_str=mat2str(B,4)

B_str=

[0.95010.60680.89130.4565;0.23110.4860.76210.0185]

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

eval(Expression)

ans=

0.38670.54510.41010.6335

0.79370.61510.46670.9817

【例3.1-6】综合例题:

在MATLAB计算生成的图形上标出图名和最大值点坐标。

(见图3.1-1)

clear

a=2;

w=3;

t=0:

0.01:

10;

y=exp(-a*t).*sin(w*t);

[y_max,i_max]=max(y);

t_text=['t=',num2str(t(i_max))];

y_text=['y=',num2str(y_max)];

max_text=char('maximum',t_text,y_text);

tit=['y=exp(-',num2str(a),'t)*sin(',num2str(w),'t)'];

plot(t,zeros(size(t)),'k')

holdon

plot(t,y,'b')

plot(t(i_max),y_max,'r.','MarkerSize',20)

text(t(i_max)+0.3,y_max+0.05,max_text)

title(tit),xlabel('t'),ylabel('y'),holdoff

图3.1-1字符串运用示意图

.1.4串转换函数

【例3.1-7】fprintf,sprintf,sscanf的用法示例。

rand('state',0);a=rand(2,2);

s1=num2str(a)

s_s=sprintf('%.10e\n',a)

s1=

0.950130.60684

0.231140.48598

s_s=

9.5012928515e-001

2.3113851357e-001

6.0684258354e-001

4.8598246871e-001

fprintf('%.5g\\',a)

0.95013\0.23114\0.60684\0.48598\

s_sscan=sscanf(s_s,'%f',[3,2])

s_sscan=

0.95010.4860

0.23110

0.60680

.1.5串操作函数

.2元胞数组

.2.1元胞数组的创建和显示

10一元胞标识寻访和内容编址寻访的不同

10二元胞数组的创建和显示

【例3.2-1】本例演示:

元胞数组的创建。

C_str=char('这是','元胞数组创建算例1');

R=reshape(1:

9,3,3);

Cn=[1+2i];

S_sym=sym('sin(-3*t)*exp(-t)');

A(1,1)={C_str};A(1,2)={R};A(2,1)={Cn};A(2,2)={S_sym};

A

A=

[2x10char][3x3double]

[1.0000+2.0000i][1x1sym]

B{1,1}=C_str;B{1,2}=R;B{2,1}=Cn;B{2,2}=S_sym;

celldisp(B)

B{1,1}=

这是

元胞数组创建算例1

B{2,1}=

1.0000+2.0000i

B{1,2}=

147

258

369

B{2,2}=

-sin(3*t)*exp(-t)

【例3.2-2】元胞数组在存放和操作字符串上的应用。

a='MATLAB5';b='introducesnewdatatypes:

';

c1='◆Multidimensionalarray';c2='◆User-definabledatastructure';

c3='◆Cellarrays';c4='◆Characterarray';

c=char(c1,c2,c3,c4);

C={a;b;c};

disp([C{1:

2}])

disp('')

disp(C{3})

MATLAB5introducesnewdatatypes:

◆Multidimensionalarray

◆User-definabledatastructure

◆Cellarrays

◆Characterarray

.2.2元胞数组的扩充、收缩和重组

【例3.2-3】元胞数组的扩充。

C=cell

(2);

C(:

1)={char('Another','textstring');10:

-1:

1}

C=

[2x11char][]

[1x10double][]

AC=[AC]

A_C=[A;C]

AC=

[2x10char][3x3double][2x11char][]

[1.0000+2.0000i][1x1sym][1x10double][]

A_C=

[2x10char][3x3double]

[1.0000+2.0000i][1x1sym]

[2x11char][]

[1x10double][]

【例3.2-4】cellplot能用图形(图3.2-1)形象化地表示元胞数组的内容。

(A_C取自上例)

cellplot(A_C,'legend')

图3.2-1元胞数组A_C的形象化结构图

【例3.2-5】元胞数组的收缩和重组。

A_C(3,:

)=[]

A_C=

[2x10char][3x3double]

[1.0000+2.0000i][1x1sym]

[1x10double][]

R_A_C=reshape(A_C,2,3)

R_A_C=

[2x10char][1x10double][1x1sym]

[1.0000+2.0000i][3x3double][]

.2.3元胞数组内容的调取

【例3.2-6】元胞数组内容的调取示例。

f1=R_A_C(1,3)

class(f1)

f1=

[1x1sym]

ans=

cell

f2=R_A_C{1,3}

class(f2)

f2=

sin(-3*t)*exp(-t)

ans=

sym

f3=R_A_C{1,1}(:

[1256])

f3=

这是

元胞创建

[f4,f5,f6]=deal(R_A_C{[1,3,4]})

f4=

这是

元胞数组创建算例1

f5=

10987654321

f6=

147

258

369

.3构架数组

.3.1构架数组的直接创建法及显示

【例3.3-1】本例通过温室数据(包括温室名、容积、温度、湿度等)演示:

单构架的创建和显示。

green_house.name='一号房';

green_house.volume='2000立方米';

green_house.parameter.temperature=[31.230.431.628.7

29.731.130.929.6];

green_house.parameter.humidity=[62.159.557.761.5

62.061.959.257.5];

green_house

green_house=

name:

'一号房'

volume:

'2000立方米'

parameter:

[1x1struct]

green_house.parameter

ans=

temperature:

[2x4double]

humidity:

[2x4double]

green_house.parameter.temperature

ans=

31.200030.400031.600028.7000

29.700031.100030.900029.6000

【例3.3-2】本例演示构架数组的创建和显示,并利用构架数组保存一个温室群的数据。

本例的运行以例3.3.1-1为先导。

green_house(2,3).name='六号房';

green_house

green_house=

2x3structarraywithfields:

name

volume

parameter

green_house(2,3)

ans=

name:

'六号房'

volume:

[]

parameter:

[]

.3.2利用构造函数创建构架数组

【例3.3-3】利用构造函数struct,建立温室群的数据库。

a=cell(2,3);

green_house_1=struct('name',a,'volume',a,'parameter',a(1,2))

green_house_1=

2x3structarraywithfields:

name

volume

parameter

green_house_2=struct('name',a,'volume',[],'parameter',[])

green_house_2=

2x3structarraywithfields:

name

volume

parameter

green_hopuse_3(2,3)=struct('name',[],'volume',[],'parameter',[])

green_hopuse_3=

2x3structarraywithfields:

name

volume

parameter

a1={'六号房'};a2={'3200立方米'};

green_house_4(2,3)=struct('name',a1,'volume',a2,'parameter',[]);

T6=[31.2,30.4,31.6,28.7;29.7,31.1,30.9,29.6];

green_house_4(2,3).parameter.temperature=T6;

green_house_4

ans=

2x3structarraywithfields:

name

volume

parameter

.3.3数值运算操作和函数对构架数组的应用

【例3.3-4】数值运算操作和函数在构架域上的作用。

n_ex=5;

fork=1:

n_ex,ex(k).f=(k-1)*n_ex+[1:

5];end

ex

ex=

1x5structarraywithfields:

f

disp([blanks(10)'构架域中内容'])

fork=1:

n_ex,disp(ex(k).f),end

构架域中内容

12345

678910

1112131415

1617181920

2122232425

class(ex

(1).f)

ans=

double

sum_f=zeros(1,5)

fork=1:

n_ex,sum_f=sum_f+ex(k).f;end,sum_f

sum_f=

5560657075

disp([blanks(20)'ex.f的平方根值'])

fork=1:

n_ex,disp(sqrt(ex(k).f)),end

ex.f的平方根值

1.00001.41421.73212.00002.2361

2.44952.64582.82843.00003.1623

3.31663.46413.60563.74173.8730

4.00004.12314.24264.35894.4721

4.58264.69044.79584.89905.0000

.4关于数据类型的归纳性说明

习题3

4.1字符串数组

4.1.1字符串入门

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

clear%清除所有内存变量

a=12345.6789%给变量a赋数值标量

class(a)%对变量a的类别进行判断

a_s=size(a)%数值数组a的“大小”

a=

1.2346e+004

ans=

double

a_s=

11

b='S'%给变量b赋字符标量(即单个字符)

class(b)%对变量b的类别进行判断

b_s=size(b)%符号数组b的“大小”

b=

S

ans=

char

b_s=

11

whos%观察变量a,b在内存中所占字节

NameSizeBytesClass

a1x18doublearray

a_s1x216doublearray

ans1x48chararray

b1x12chararray

b_s1x216doublearray

Grandtotalis10elementsusing50bytes

4.1.2串数组的属性和标识

【*例4.1.2-1】本例演示:

串的基本属性、标识和简单操作。

(1)创建串数组

a='Thisisanexample.'

a=

Thisisanexample. 

(2)串数组a的大小

size(a)

ans=

119

(3)串数组的元素标识

a14=a(1:

4)%提出一个子字符串

ra=a(end:

-1:

1)%字符串的倒排

a14=

This

ra=

.elpmaxenasisihT

(4)串数组的ASCII码

ascii_a=double(a)%产生ASCII码

ascii_a=

Columns1through12

8410410511532105115329711032101

Columns13through19

1209710911210810146

char(ascii_a)%把ASCII码变回字符串

ans=

Thisisanexample.

(5)对字符串ASCII码数组的操作

%使字符串中字母全部大写

w=find(a>='a'&a<='z');%找出串数组a中,小写字母的元素位置。

ascii_a(w)=ascii_a(w)-32;%大小写字母ASCII值差32.用数值加法改变部分码值。

char(ascii_a)%把新的ASCII码翻成字符

ans=

THISISANEXAMPLE.

(6)中文字符串数组

A='这是一个算例。

';%创建中文字符串

A_s=size(A)%串数组的大小

A56=A([56])%取串的子数组

ASCII_A=double(A)%获取ASCII码

A_s=

17

A56=

算例

ASCII_A=

Columns1through6

547545191153947473505219549405

Column7

41379

char(ASCII_A)%把ASCII码翻译成字符

ans=

这是一个算例。

(7)创建带单引号的字符串

b='Example''4.1.2-1'''

b=

Example'4.1.2-1'

(8)由小串构成长串

ab=[a(1:

7),'',b,'.']%这里第2个输入为空格串

ab=

ThisisExample'4.1.2-1'.

4.1.3复杂串数组的创建

4.1.3.1多行串数组的直接创建

【*例4.1.3.1-1】多行串数组的直接输入示例。

clear

S=['Thisstringarray'

'hasmultiplerows.']

S=

Thisstringarray

hasmultiplerows.

size(S)

ans=

18

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

【*例4.1.3.2-1】演示:

用专门函数char,str2mat,strvcat创建多行串数组示例。

S1=char('Thisstringarray','hastworows.')

S1=

Thisstringarray

hastworows.

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

S2=

字符

串数组

由4行组成

S3=strvcat('这','字符','串数组','','由4行组成')%“空串”会产生一个空格行

S3=

字符

串数组

由4行组成

size(S3)

ans=

55

【*例4.1.3.2-1】的补充

(1)  创建一个二维字符数组animal

>>Animal=[‘dog’;’monkey’];

?

?

?

Errorusing==>vertcat

CATargumentsdimensionsarenotc

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

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1