|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
* q; L H: v( X
一、实验任务和目的 " Z! q+ v7 R) u, ~
1. 掌握Matlab的字符串常用函数及其操作方法。
* J5 A2 |( D: ]) G) Y U2. 掌握Matlab的结构体的基本操作方法。 / c- A/ @; b8 m* E0 w/ ]+ i/ J
3. 掌握Matlab的元胞数组的基本操作方法。 5 V9 u) k, [9 O0 ^9 O1 \
二、实验内容
' f* d- {0 \; s- N8 U/ [1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。
6 t5 F' I* e) t, t2 g2. 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,利用字符串处理函数,将其用空格连接在一起,并字母转换为大写,并将7.1替换为2016a。
, M% Y4 w q1 H" C+ P3. 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.’,对该字符串做如下处理:
' R0 h8 z3 u! ?; K) H(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。
8 u" C( H. X5 v# e$ v+ |(2)统计字符串中的数字和字母的个数。
. E6 j' s2 g' u" ^9 @, b(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。 7 N( E2 D8 }4 Y/ W
4. 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。 " l( j) T% a9 n$ Q/ |3 K
5. 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。) Y! M+ h" t6 T8 w6 {2 M
3 }& A. h6 D3 Z3 E% \
三、实验过程和结果
@. [$ a3 y$ k6 x0 ^( C1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。0 x3 D0 w, r% j. y! i
; L: p' `) g% B4 r, _% E5 K- >> Str=['hopes, dreams, hold up, old up'];
- p=findstr(Str,'O')
- p =
- []
- >> length(p)
- ans =
- 0
4 v$ c! L! A1 e' |$ F1 K8 O
0 v8 U! ^# Q, b
. J! f3 p! F+ P1 h3 b2 . 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,,并字母转换为大写,并将7.1替换为2016a。! Y" T+ O* J# }( ]/ W
4 r+ z- M, O+ a* U: G9 N6 O0 K+ `
- >> 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 MATLAB2016a8 q) d! z% ?$ e A! m- y/ R
8 h" B: T6 w J
7 V+ _0 b3 m5 K# s3 . 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.’,对该字符串做如下处理: / b- M" w; b; o: X3 P8 I, W
(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。2 H( R% s+ M; K3 p2 j# Q, m$ E
* l, P% d1 l% n0 s- >> 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.2 b& S# p" O% X
7 Q% k0 ~* ?) E2 p1 M: M% y
& B& e7 U8 f: B* G( m
(2)统计字符串中的数字和字母的个数。2 D8 J% P) y( h, s) [2 M
! O0 k. d$ r" \/ }2 a6 B
- >> 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; L" |: W5 b0 p/ B: a* W
* Z6 s9 K* a( }$ ^( P: ]/ U
, i7 p; `8 ]0 z) {7 y" J, c& X0 A( I; e(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。# s# k- [, u3 h& ]" g
1 l, \4 t7 P- e, e! n/ k! 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.';
- S=strrep(Str,' ','')
- k=find(S>='0'&S<='9');
- S(k)='';
- revch=S(end:-1:1)
- S =
- 1Theexistingresearchisaboutlocationtrackingeithercompletelyindoororaltogetheronopenair2byutilizingvarioussensorsandproceduresbasedoninter-networkingorinternetofthings.
- revch =
- .sgnihtfotenretnirognikrowten-retninodesabserudecorpdnasrosnessuoiravgnizilituybrianeponorehtegotlaroroodniyletelpmocrehtiegnikcartnoitacoltuobasihcraesergnitsixeehT: ~# X$ z: g, f% V5 a# P
- f& _% M" `- j+ R4 N2 ]
) n/ n& k6 _2 G) b
4 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。
+ O+ X' @* F8 {; @8 _: W. n' y8 @ q
这里假设一个班有三名同学(不好意思,这个我暂时不会); P2 W/ A3 [3 d* Z0 s
' I6 B" x& P. G6 `" c! {0 ?
5 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。: M+ k) x$ ?. J$ R. |' F. ]
0 J4 H" a( Y% g0 c+ @- >> 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). p% _- o3 J2 h* h3 M: a% \! N( p
% k, i2 `, [" h# R
& r( _" k- `/ @% u; Y( u& c) L) v4 r) H
四、实验总结和心得 z& M% I" I3 j
掌握了字符串的查找,连接,删除,倒置,替换等一系列操作 8 h% G& t2 W. V r2 n8 s
掌握了结构数组和元胞数组的用法
! q' W" }2 |( u
' S. X3 G5 z4 M# {5 S! J5 q9 h5 e B2 ^! e3 m* Y/ ~ E
" P& l4 q* w: o% G) s! K5 }0 _9 _3 W$ n$ t% I
|
|