MATLAB第三章字符串.docx

上传人:b****4 文档编号:5472307 上传时间:2023-05-08 格式:DOCX 页数:30 大小:36.07KB
下载 相关 举报
MATLAB第三章字符串.docx_第1页
第1页 / 共30页
MATLAB第三章字符串.docx_第2页
第2页 / 共30页
MATLAB第三章字符串.docx_第3页
第3页 / 共30页
MATLAB第三章字符串.docx_第4页
第4页 / 共30页
MATLAB第三章字符串.docx_第5页
第5页 / 共30页
MATLAB第三章字符串.docx_第6页
第6页 / 共30页
MATLAB第三章字符串.docx_第7页
第7页 / 共30页
MATLAB第三章字符串.docx_第8页
第8页 / 共30页
MATLAB第三章字符串.docx_第9页
第9页 / 共30页
MATLAB第三章字符串.docx_第10页
第10页 / 共30页
MATLAB第三章字符串.docx_第11页
第11页 / 共30页
MATLAB第三章字符串.docx_第12页
第12页 / 共30页
MATLAB第三章字符串.docx_第13页
第13页 / 共30页
MATLAB第三章字符串.docx_第14页
第14页 / 共30页
MATLAB第三章字符串.docx_第15页
第15页 / 共30页
MATLAB第三章字符串.docx_第16页
第16页 / 共30页
MATLAB第三章字符串.docx_第17页
第17页 / 共30页
MATLAB第三章字符串.docx_第18页
第18页 / 共30页
MATLAB第三章字符串.docx_第19页
第19页 / 共30页
MATLAB第三章字符串.docx_第20页
第20页 / 共30页
亲,该文档总共30页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

MATLAB第三章字符串.docx

《MATLAB第三章字符串.docx》由会员分享,可在线阅读,更多相关《MATLAB第三章字符串.docx(30页珍藏版)》请在冰点文库上搜索。

MATLAB第三章字符串.docx

MATLAB第三章字符串

第三章

字符串、元胞和构架数组

MATLAB6.x版的内建数据类型(Built-indatatype)就有5种以上,此外还有许多其他专门设计的类(Class),如符号类、内联函数类、控制工具包中的线性时不变模型类、神经网络类等。

就程序设计而言,MATLAB6.x版采用了面向对象编程技术。

数据和编程的改变使用户能更简捷而自然地解决复杂的计算问题(如符号计算问题、多变量控制系统问题、神经网络问题)。

本章内容根据MATLAB6.5编写,但绝大部分内容适用于其他MATLAB6.x版本。

第二章介绍了数值数组(NumericArray),这是读者比较熟悉的数据类型。

本章将集中讲述另外三类数据:

字符串数组(CharacterStringArray)、元胞数组(Cellarray)和构架数组(Structurearray)。

它们之间的基本差别见表3-1。

表3-1四种数据类型基本构成比较表

数组类型

基本组分

组分内涵

基本组分占用字节数

数值数组

元素

双精度实数标量

或双精度复数标量

8

16

字符串数组

元素

字符

2

元胞数组

元胞

可以存放任何类型、任何大小的数据。

不定

构架数组

构架

只有挂接在构架上的“域”才能存放数据。

数据可以是任何类型、任何大小。

不定

3.1字符串数组

3.1.1字符串入门

【例3.1.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

3.1.2串数组的属性和标识

【例3.1.2-1】本例演示:

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

(1)

a='Thisisanexample.'

a=

Thisisanexample.

(2)

size(a)

ans=

119

(3)

a14=a(1:

4)

ra=a(end:

-1:

1)

a14=

This

ra=

.elpmaxenasisihT

(4)

ascii_a=double(a)

ascii_a=

Columns1through12

8410410511532105115329711032101

Columns13through19

1209710911210810146

char(ascii_a)

ans=

Thisisanexample.

(5)

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

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

char(ascii_a)

ans=

THISISANEXAMPLE.

(6)

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=

这是一个算例。

(7)

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

b=

Example'3.1.2-1'

(8)

ab=[a(1:

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

ab=

ThisisExample'3.1.2-1'.

3.1.3复杂串数组的创建

3.1.3.1多行串数组的直接创建

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

clear

S=['Thisstringarray'

'hasmultiplerows.']

S=

Thisstringarray

hasmultiplerows.

size(S)

ans=

218

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

【例3.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

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

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

(1)

A=eye(2,4);

A_str1=int2str(A)

A_str1=

1000

0100

(2)

rand('state',0)

B=rand(2,4);

B3=num2str(B,3)

B3=

0.950.6070.8910.456

0.2310.4860.7620.0185

(3)

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.3.3-2】综合例题:

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

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))];%<7>

y_text=['y=',num2str(y_max)];%<8>

max_text=char('maximum',t_text,y_text);%<9>

%

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

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)%<16>

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

图3.1-1

3.1.3.4利用元胞数组创建复杂字符串

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

a='MATLAB6.x';b='includesnewdatatypes:

';

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

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

c5='◆Functionhandle';

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

C={a;b;c};%<5>

disp([C{1:

2}])%<6>

disp('')%

disp(C{3})%<8>

MATLAB6.xincludesnewdatatypes:

◆Multidimensionalarray

◆User-definabledatastructure

◆Cellarrays

◆Characterarray

◆Functionhandle

3.1.4串转换函数

【例3.1.4-1】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

3.1.5串操作函数

3.2元胞数组

3.2.1元胞数组的创建和显示

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

3.2.1.2元胞数组的创建和显示

【例3.2.1.2-1】本例演示:

元胞数组的创建。

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

R=reshape(1:

9,3,3);

Cn=[1+2i];

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

(1)直接创建法之一

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]

(2)直接创建法之二

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元胞数组的扩充、收缩和重组

【例3.2.2-1】元胞数组的扩充。

(1)

C=cell

(2);

C(:

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

-1:

1}

C=

[2x11char][]

[1x10double][]

(2)

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.2-2】cellplot能用图形形象化地表示元胞数组的内容。

(A_C取自上例)

cellplot(A_C,'legend')

图3.2-1

【例3.2.2-3】元胞数组的收缩和重组。

(1)

A_C(3,:

)=[]

A_C=

[2x10char][3x3double]

[1.0000+2.0000i][1x1sym]

[1x10double][]

(2)

R_A_C=reshape(A_C,2,3)

R_A_C=

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

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

3.2.3元胞数组内容的调取

【例3.2.3-1】元胞数组内容的调取示例。

(1)

f1=R_A_C(1,3)

class(f1)

f1=

[1x1sym]

ans=

cell

(2)

f2=R_A_C{1,3}

class(f2)

f2=

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

ans=

sym

(3)

f3=R_A_C{1,1}(:

[1256])

f3=

这是

元胞创建

(4)

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

f4=

这是

元胞数组创建算例1

f5=

10987654321

f6=

147

258

369

3.2.4元胞数组转换函数

【例3.2.4-1】常用元胞数组转换函示例。

(1)num2cell把数值数组转换成元胞数组

rand('state',0);

A=rand(2,3,2)

C1=num2cell(A)

A(:

:

1)=

0.95010.60680.8913

0.23110.48600.7621

A(:

:

2)=

0.45650.82140.6154

0.01850.44470.7919

C1(:

:

1)=

[0.9501][0.6068][0.8913]

[0.2311][0.4860][0.7621]

C1(:

:

2)=

[0.4565][0.8214][0.6154]

[0.0185][0.4447][0.7919]

C2=num2cell(A,1)

C2(:

:

1)=

[2x1double][2x1double][2x1double]

C2(:

:

2)=

[2x1double][2x1double][2x1double]

C3=num2cell(A,[2,3])

C3=

[1x3x2double]

[1x3x2double]

(2)

clear,x=zeros(4,5);

x(:

)=1:

20

C4=mat2cell(x,[22],[32])

celldisp(C4)

x=

1591317

26101418

37111519

48121620

C4=

[2x3double][2x2double]

[2x3double][2x2double]

C4{1,1}=

159

2610

C4{2,1}=

3711

4812

C4{1,2}=

1317

1418

C4{2,2}=

1519

1620

(3)

D=cell2mat(C4(1,:

))

D=

1591317

26101418

3.3构架数组

3.3.1构架数组的创建和显示

3.3.1.1直接创建法及显示

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

单构架的创建和显示。

(1)

green_house.name='一号房';%<1>

green_house.volume='2000立方米';%<2>

green_house.parameter.temperature=[31.230.431.628.7

29.731.130.929.6];%<3>

green_house.parameter.humidity=[62.159.557.761.5

62.061.959.257.5];%<4>

(2)显示“单构架”结构和内容

green_house%<5>

green_house=

name:

'一号房'

volume:

'2000立方米'

parameter:

[1x1struct]

green_house.parameter%<6>

ans=

temperature:

[2x4double]

humidity:

[2x4double]

green_house.parameter.temperature%<7>

ans=

31.200030.400031.600028.7000

29.700031.100030.900029.6000

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

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

(1)

green_house(2,3).name='六号房';%<1>

(2)

green_house%<2>

green_house=

2x3structarraywithfields:

name

volume

parameter

green_house(2,3)%<3>

ans=

name:

'六号房'

volume:

[]

parameter:

[]

3.3.1.2利用构造函数创建构架数组

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

(1)

a=cell(2,3);

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

green_house_1=

2x3structarraywithfields:

name

volume

parameter

(2)

green_house_2=struct('name',a,'volume',[],'parameter',[])%<3>

green_house_2=

2x3structarraywithfields:

name

volume

parameter

(3)

green_hopuse_3(2,3)=struct('name',[],'volume',[],'parameter',[])%<4>

green_hopuse_3=

2x3structarraywithfields:

name

volume

parameter

(4)

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

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

T6=[31.2,30.4,31.6,28.7;29.7,31.1,30.9,29.6];%<7>

green_house_4(2,3).parameter.temperature=T6;%<8>

green_house_4

green_house_4=

2x3structarraywithfields:

name

volume

parameter

 

3.3.2构架数组域中内容的调取和设置

【例3.3.2-1】本例目的:

一,演示函数fieldnames,getfield,setfield的使用方法;二,让读者感受到构架数组对应用工具包的影响;三,演示struct函数把“对象”转换为构架的应用。

本例为获得一个演练的构架,借助Toolboxcontrol工具包中的tf函数,先产生一个用传递函数描写的LTI线性时不变2输入2输出系统

(1)

Stf=tf({3,2;[41],1},{[132],[111];[1221],[10]})

Transferfunctionfrominput1tooutput...

3

#1:

-------------

s^2+3s+2

4s+1

#2:

---------------------

s^3+2s^2+2s+1

Transferfunctionfrominput2tooutput...

2

#1:

-----------

s^2+s+1

1

#2:

-

s

(2)

SSTF=struct(Stf)

SSTF=

num:

{2x2cell}

den:

{2x2cell}

Variable:

's'

lti:

[1x1lti]

(3)

FN=fieldnames(SSTF)

class(FN)

FN=

'num'

'den'

'Variable'

'lti'

ans=

cell

(4)

FC=getfield(SSTF,'den',{2,1})

FC{1}

poly2str(FC{1},'s')

FC=

[1x4double]

ans=

1221

ans=

s^3+2s^2+2s+1

(5)

SSTF.num{2,1}

SSTF=setfield(SSTF,'num',{2,1},{[131]});

SSTF.num{2,1}

ans=

0041

ans=

131

3.3.3构架数组操作深入

3.3.3.1构架数组的扩充和收缩

【例3.3.3.1-1】本例演示构架数组SSTF的扩充和收缩。

(本例以例3.3.2-1的运行为基础。

(1)

size(SSTF)

ans=

11

(2)

SSTF(2,2)=struct(tf(1,[11]))

size(SSTF)

SSTF=

2x2structarraywithfields:

num

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

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2