|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
m+ t) E5 k4 }* m: s一、实验任务和目的 3 o" a j. e {" G" U
1. 掌握Matlab的字符串常用函数及其操作方法。
$ `! C b6 \9 g! P2. 掌握Matlab的结构体的基本操作方法。
9 T; h# I& ~9 f3. 掌握Matlab的元胞数组的基本操作方法。
0 y- y5 b2 L% y- o+ T' K) s1 ?二、实验内容 5 I% O2 U9 u2 @& ]" k7 O
1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。 ) o1 a3 y9 c7 e% F
2. 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,利用字符串处理函数,将其用空格连接在一起,并字母转换为大写,并将7.1替换为2016a。 ; `- E7 i+ x6 A( r- W ^
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.’,对该字符串做如下处理:
; r1 y- l: P3 q" E8 ]$ r$ H Q(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。
6 n0 s- G% {& |/ T( t0 r(2)统计字符串中的数字和字母的个数。 4 T* f( c$ B1 v. A; Z
(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。
/ ]1 T- @' Y) ]! d4. 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。
6 `1 F2 n7 Q+ i' O5. 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。
" d8 I& O: _* `$ t
# u% _2 g1 u* C J9 e( F! a三、实验过程和结果 4 D) {. z; f1 @ B: b
1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。/ s+ Q. U: G( @" [- D9 n
7 Q; W" B$ ~. q E4 z# ^- >> Str=['hopes, dreams, hold up, old up'];
- p=findstr(Str,'O')
- p =
- []
- >> length(p)
- ans =
- 0
; h5 k: L: `4 B) J
' p: Y% F( X. o/ b; X
( X4 {( g; D* @" I( T7 q& R2 . 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,,并字母转换为大写,并将7.1替换为2016a。& C3 u3 q' {- w
$ N6 r- l8 b4 a1 N
- >> 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 MATLAB2016a5 A- G/ n" n+ G
9 n# C( r1 N, I7 x/ Y3 s/ S" ^$ Q, h. o% M
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.’,对该字符串做如下处理:
* V) B4 T7 Z D/ M. \. ?& D6 z(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。
7 ]" e: s" b) q7 ^( [$ k6 e3 J
6 \* R5 f! s0 y- >> 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.
: h a g/ t! H
( d4 N/ S- {! d" l r& {3 E
" } S6 g" J$ e( j(2)统计字符串中的数字和字母的个数。* s4 |3 [+ D* p4 ~! m. n
0 ?0 c4 s' u. P4 L0 x3 G& k- >> 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 d8 @9 n Y+ V: E# J
+ T' k0 j! M7 E' p& @( K: a
* a- H- ?% l* t3 d4 H
(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。
+ I% e* @3 w" n: u& C3 ^9 [
|! d; G' Y) I3 o5 V3 B/ a+ \& 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.';
- S=strrep(Str,' ','')
- k=find(S>='0'&S<='9');
- S(k)='';
- revch=S(end:-1:1)
- S =
- 1Theexistingresearchisaboutlocationtrackingeithercompletelyindoororaltogetheronopenair2byutilizingvarioussensorsandproceduresbasedoninter-networkingorinternetofthings.
- revch =
- .sgnihtfotenretnirognikrowten-retninodesabserudecorpdnasrosnessuoiravgnizilituybrianeponorehtegotlaroroodniyletelpmocrehtiegnikcartnoitacoltuobasihcraesergnitsixeehT
- E' S2 ]6 b! i' q
% \ V: J$ x; Y2 Q0 ^2 P% B; l+ ~4 e4 X7 z6 E9 x
4 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。
S5 A: V; w; C0 S
! G0 d7 F* w' W/ C4 g5 }( `这里假设一个班有三名同学(不好意思,这个我暂时不会)8 F: E6 \7 g* @* U2 j
5 I& A, A1 g% q0 N# i
5 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。
W5 `& @$ F! p9 P& v
% [% \2 m$ V0 n' l* J& J' [8 x! G- >> 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)# Z7 y, m% f' b9 ?0 f* ]* z
( j# B% a6 B5 z5 R
9 t5 k0 L9 z* m S$ h
, x8 H7 R+ Q% b0 @- D四、实验总结和心得
* K, b0 ]1 g5 W2 K$ F$ T# F* {, X掌握了字符串的查找,连接,删除,倒置,替换等一系列操作
7 F* Y- G5 O0 r$ [6 c! x掌握了结构数组和元胞数组的用法
0 q- N& K! f6 j" ~) k# Y3 w2 K+ ?& t$ O9 M/ C$ S- Q: L6 Y
5 ^8 ^* j' n; V$ l- p9 D
/ P" J0 J% P) D
% a+ {$ t$ \3 D0 {+ r3 d
|
|