|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
8 r7 u4 ?2 F9 J- Y+ G& f
一、实验任务和目的 9 h' c' C5 A+ }0 I' Z* F
1. 掌握Matlab的字符串常用函数及其操作方法。 ! u) f$ y4 L3 `9 i$ ^$ @- D, _" G
2. 掌握Matlab的结构体的基本操作方法。
8 o. e; H' t2 |) W( z% S3. 掌握Matlab的元胞数组的基本操作方法。
" D* A4 T9 L6 |3 ?二、实验内容 ' S. w! q& |) y% @! O
1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。 / h0 t3 m. K2 a
2. 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,利用字符串处理函数,将其用空格连接在一起,并字母转换为大写,并将7.1替换为2016a。 9 O5 Q5 x7 D, `. k+ s( y
3. 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.’,对该字符串做如下处理: 4 S( v$ C2 M$ c# o
(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。 - v: x6 P/ h/ Q/ z4 s' H4 @ c
(2)统计字符串中的数字和字母的个数。 4 ~. z- T \& X, e* ~
(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。 7 A# x1 b( U2 Q. ^' B$ R' y+ H
4. 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。
* g6 V; q* `- \% L* \: k6 S5. 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。
. x1 e, d5 P8 }
/ J8 ^( B( j, i7 f& {; v! u6 d三、实验过程和结果
) B6 L) ^0 C+ n3 a" V. k7 H1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。
! D, g' E( K/ y, f7 f' Q( N2 D+ Z
- >> Str=['hopes, dreams, hold up, old up'];
- p=findstr(Str,'O')
- p =
- []
- >> length(p)
- ans =
- 0
7 p4 R( s$ t$ v; v5 T5 a5 F & P' w' N( l! G( }" b! N1 G5 ]
7 _, o$ r9 f A) F/ b' k; ?- f2 . 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,,并字母转换为大写,并将7.1替换为2016a。
9 X7 y( e( W+ u# Y0 l9 h: d) x1 O' H' x9 N( n. p
- >> 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, p5 [ y0 C4 \3 W1 v! s
- E2 Q2 i# R( C+ K; V2 c$ r y- s- l2 e# O7 M2 m. }+ g
3 . 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.’,对该字符串做如下处理:
1 @% X" v5 e( P. P(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。
& z- b X2 C6 |) N& d p3 Q. ]5 t& ~1 Z3 e
- >> 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.3 q: L' i8 I3 `/ U) @9 ]
5 b4 E' Z X9 Z7 K* v6 ]3 @- m
) A% l0 n! ^6 c$ C' o$ b4 @
(2)统计字符串中的数字和字母的个数。
- K* Q' C: O, j8 U9 Z% l. p2 C2 c. f
- >> 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( ]8 ]! O5 L2 t {, u! V8 p
: X2 W* X; } A9 k: s2 q! J i; f: W
! I6 a, y2 |4 N0 h(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。
9 E2 g) B+ H9 a; p
( H; A% J$ q& R8 P' v- >> 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-retninodesabserudecorpdnasrosnessuoiravgnizilituybrianeponorehtegotlaroroodniyletelpmocrehtiegnikcartnoitacoltuobasihcraesergnitsixeehT1 Z+ p2 a) `: s4 \: K
7 ?0 |" ?6 F7 j
& F! `) N0 f& B, m" z; C r& Y4 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。
) ~: q4 t- ]/ ]8 c2 ]! D, l8 j3 \3 P$ M: s2 r S
这里假设一个班有三名同学(不好意思,这个我暂时不会)
0 d9 @- V5 k& ^. B
" X- P- u% H/ D- u+ x% G5 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。9 l% e' l% W! B2 c$ W
1 w1 b+ `& \3 s4 T6 U2 Z- >> 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)
8 ~( Y, L/ n) ?! D- a ( N9 E9 ?# c5 u1 u8 `6 F- h9 P
: a7 }: L6 C! j, ]% {/ `0 L
0 W! s1 e8 f* M, [" w" T四、实验总结和心得 8 @. f" @' H+ c( R8 f
掌握了字符串的查找,连接,删除,倒置,替换等一系列操作 " S( f+ {1 A8 @1 J0 o
掌握了结构数组和元胞数组的用法1 b3 ^% m H# m% c. M6 s5 @
5 q5 @! [! ]) K( p, u- W. [
' {! n+ B) z! U! _4 O& t& M" h1 n- @
1 \0 ` B/ o; c( |/ C( V" V- X |
|