|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
; s& D- ]( E9 g3 D
一、实验任务和目的
9 E! W0 W: V! l9 b$ H1. 掌握Matlab的字符串常用函数及其操作方法。
0 N ]) V' f8 c) [' A* v2. 掌握Matlab的结构体的基本操作方法。 ; d! V( M0 S) D* m3 H }( e
3. 掌握Matlab的元胞数组的基本操作方法。 0 Y7 G1 `9 l9 ]. q6 P* v, R% {5 f! u
二、实验内容
/ F, f; w7 S9 ~1 ?: s u& H1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。
5 Y( r, ~! o8 R: i/ P9 T& {$ \2. 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,利用字符串处理函数,将其用空格连接在一起,并字母转换为大写,并将7.1替换为2016a。
. C. w/ S6 S% |9 t2 s. W0 J1 X3. Str=’ 1 The existing research is about location tracking either completely indoor or altogether on open air 2 by utilizing various sensors and procedures based on inter-networking or internet of things.’,对该字符串做如下处理: ! {% F) ^* f. O& p3 B4 Q. l4 @8 J2 u
(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。
5 B! H6 l$ m' q(2)统计字符串中的数字和字母的个数。
- y0 O% ]1 i! Y9 ]" o! o/ b(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。 4 o# [( {1 p/ n4 G0 W
4. 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。 * T2 _" q# m/ k' A( s
5. 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。
& [9 k" c. A, S0 q+ I( B
+ g% N; U$ m; k4 {三、实验过程和结果
3 F8 X! P. H% d4 } c" \1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。
' ]3 b& O1 z: y
, P0 D, o4 U: E" z8 a1 F- >> Str=['hopes, dreams, hold up, old up'];
- p=findstr(Str,'O')
- p =
- []
- >> length(p)
- ans =
- 07 q7 i& K* X n. P3 `
. d' N: f# y" B
- R) f' l* S, g0 D
2 . 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,,并字母转换为大写,并将7.1替换为2016a。! M. T" I! d2 g; C
2 e2 T8 g+ Y+ Q( N. G' ^# A- >> s1='i';
- >> s2='love';
- >> s3='matlab7.1';
- >> s4=strcat(s1,32,s2,32,s3);
- >> k=find(s4>='a'&s4<='z');
- >> s4(k)=s4(k)-'a'+'A';
- >> s4
- s4 =
- I LOVE MATLAB7.1
- >> strrep(s4,'7.1','2016a')
- ans =
- I LOVE MATLAB2016a
" Y4 o5 L/ o: z, |( C: {5 R1 D/ t* y
8 W2 J! ?* d' D0 y1 ~/ @/ G
3 S9 P( e. {+ S# u9 e3 e3 . Str=’ 1 The existing research is about location tracking either completely indoor or altogether on open air 2 by utilizing various sensors and procedures based on inter-networking or internet of things.’,对该字符串做如下处理: 7 }% ^" f) A$ _: s( L) z
(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。$ q) U9 |& A$ T. o
% j. |& J. ]/ m$ x- >> Str=' 1 The existing research is about location tracking either completely indoor or altogether on open air 2 by utilizing various sensors and procedures based on inter-networking or internet of things.';
- k=findstr(Str,' ');
- l=length(k);
- for j=1:l
- if(Str(k(j)+1)>='a'&Str(k(j)+1)<='z')
- Str(k(j)+1)=Str(k(j)+1)-'a'+'A';
- end
- end
- >> Str
- Str =
- 1 The Existing Research Is About Location Tracking Either Completely Indoor Or Altogether On Open Air 2 By Utilizing Various Sensors And Procedures Based On Inter-networking Or Internet Of Things.
( J+ @% [/ G: l* ^
2 r$ H+ W$ e# j' c7 s# R8 l* x& g) x+ N6 o" i' {( J
(2)统计字符串中的数字和字母的个数。
4 M6 K* I9 u: \$ Y' ~ K4 @1 ]- q2 @$ ^
- >> Str=' 1 The existing research is about location tracking either completely indoor or altogether on open air 2 by utilizing various sensors and procedures based on inter-networking or internet of things.';
- >> k=find(Str>='0'&Str<='9');
- >> m=find(Str>='a'&Str<='z');
- >> length(k)
- ans =
- 2
- >> length(m)
- ans =
- 162
0 J0 R) x# x) Z$ ]4 Q5 s 7 X+ G3 p q9 w* }
6 E1 e, J3 ?( J
(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。
) N5 F, f; Q# v- K/ ^3 y6 g6 C# e0 J6 O& i4 h
- >> Str=' 1 The existing research is about location tracking either completely indoor or altogether on open air 2 by utilizing various sensors and procedures based on inter-networking or internet of things.';
- S=strrep(Str,' ','')
- k=find(S>='0'&S<='9');
- S(k)='';
- revch=S(end:-1:1)
- S =
- 1Theexistingresearchisaboutlocationtrackingeithercompletelyindoororaltogetheronopenair2byutilizingvarioussensorsandproceduresbasedoninter-networkingorinternetofthings.
- revch =
- .sgnihtfotenretnirognikrowten-retninodesabserudecorpdnasrosnessuoiravgnizilituybrianeponorehtegotlaroroodniyletelpmocrehtiegnikcartnoitacoltuobasihcraesergnitsixeehT
% t" E. ^) R! N( E8 A W( ? * q6 ^0 S2 F$ {
" [' I2 y! W; q5 h$ \, Z$ o9 T4 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。
: C0 L4 f, g% i) o8 X N
, z; a; q* ~. ^& j这里假设一个班有三名同学(不好意思,这个我暂时不会)' v* x2 S4 t: c
; }6 I, h5 O; `. Z4 Y& ?5 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。
3 \6 ]3 i( D, N+ |+ E0 e; g. e6 G4 J
- >> A=cell(2,2);
- >> A(1,1)={'i love'};
- >> A(2,1)={'you'};
- >> A(1,2)={int16(128)};
- >> A(2,2)={double(16)};
- >> cellplot(A)
; _ J; _/ g& ~4 P7 |! m$ q
! ~! J- Z0 R+ p$ D
8 b- b$ S% l T
5 q/ M4 ~! z2 I8 m: h四、实验总结和心得
+ B5 E0 p) M. _* a( i2 w掌握了字符串的查找,连接,删除,倒置,替换等一系列操作
$ N2 S d+ K+ d+ t' f1 V2 A掌握了结构数组和元胞数组的用法
( v# p# |9 q; J! [% `, @- @4 [% s/ W+ f* X! o! ] E0 s( G# K
% o4 h0 g2 E6 N
# [" X/ u5 t( F! J* D2 V
6 |# d! y q5 z
|
|