|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
, k/ w- w. H1 v( D- H一、实验任务和目的 ! S8 \ e, ]& {. g( R
1. 掌握Matlab的字符串常用函数及其操作方法。 - x u/ B/ S, T
2. 掌握Matlab的结构体的基本操作方法。
* F w. A, {- t) {% p2 A$ W, i! a3. 掌握Matlab的元胞数组的基本操作方法。
+ r" q4 w- x# N/ I; {; x二、实验内容
# }1 e+ W; ~6 A1 w0 F1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。 / S- ?% \5 h) |5 S4 K6 y
2. 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,利用字符串处理函数,将其用空格连接在一起,并字母转换为大写,并将7.1替换为2016a。
; W" Y. I: A# k7 k4 Q3. 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 s9 k; N1 M( k0 M" i(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。
: a" {! w% J9 f! [(2)统计字符串中的数字和字母的个数。
" n# ~. n E/ S4 [(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。 - q7 F5 a" P6 c" C+ ]) ]
4. 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。
" f6 P' d4 a- w& E5 n) U5. 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。
N% X1 I' B x' m+ v, ^ D, Q `! Q' Q5 x& \. s
三、实验过程和结果
; y' R6 K/ o/ I6 ^7 n$ E" Q0 Z1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。) X" S/ C7 Q+ \" q
" z4 a" H6 C5 X- >> Str=['hopes, dreams, hold up, old up'];
- p=findstr(Str,'O')
- p =
- []
- >> length(p)
- ans =
- 0
7 P5 E6 {5 A0 @1 A O ! M3 |: M$ w' g3 S
* h1 {1 o: v" v' l2 . 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,,并字母转换为大写,并将7.1替换为2016a。+ }; r( n8 [" i2 Z
, A$ Z, D/ v5 J$ n) J
- >> 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: A: q& s: A% Z- Z
j+ \! m: H, w! A/ M- n* u
; T% E. P$ ?' o7 `( r: F3 . 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 K3 F+ j0 z9 r+ l- j8 q: V$ o L(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。
0 I8 K" V$ s8 b1 p' r" I5 H1 T* ~; }& e% c" r3 A7 j6 T5 a6 n
- >> 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.
; b: K, t" M1 w
4 j1 h0 Q# T; I. T; g u# t9 W9 b% W/ m3 J' [
(2)统计字符串中的数字和字母的个数。! v: N, k; U8 [ a1 ]
/ C5 K r) y" h# j! t+ b$ C' e8 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=find(Str>='0'&Str<='9');
- >> m=find(Str>='a'&Str<='z');
- >> length(k)
- ans =
- 2
- >> length(m)
- ans =
- 162' v8 D* L1 z, w" z. E' r
# N8 Y+ X" W* m4 G( k! P9 b B$ V" \. r
(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。
( n: y/ H) x1 z5 p8 }. Y1 F
k; s5 {6 c& `' c- >> 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
+ V' k3 {; X# s3 g . X8 q2 G9 f5 [; j( S- @: n
& @3 U' ?; w s( L8 {# M
4 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。
* q7 n" \6 O. m+ Y
( s. S/ a/ Z* @$ v这里假设一个班有三名同学(不好意思,这个我暂时不会)
$ C" N. X( x8 I/ z# q. f! f7 c( l5 h" m/ o' I" J
5 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。
4 M/ w/ \8 V3 B
3 r6 @+ t9 F( K* [& 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)( l; ?" W, O# Q# B# v2 I0 m; ?7 L
1 f- C8 g6 j9 R9 W4 d
- N( g- S# w/ O/ H
, |& J w0 S P8 A( }四、实验总结和心得 9 ]2 [( V0 S) B1 Q
掌握了字符串的查找,连接,删除,倒置,替换等一系列操作 M. n6 i, }5 R
掌握了结构数组和元胞数组的用法
- i3 L3 i6 w) g) N3 J+ O- k" L. B* |. [; f5 r
$ w& ~ ?: ^' g( x; A
2 O/ v# u" m* U; v2 }/ X- T( A7 ^: y. Z5 P- w. |/ P5 @# G
|
|