|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
6 ]3 y0 d+ i% e& O' ~+ a
一、实验任务和目的 # [$ o) h8 v" l) x/ |- p2 [" \* X
1. 掌握Matlab的字符串常用函数及其操作方法。 7 z# k1 |' B- Y1 O) b
2. 掌握Matlab的结构体的基本操作方法。 / `% j F1 q' h2 ^/ ~+ x9 N
3. 掌握Matlab的元胞数组的基本操作方法。 & Q! f. v3 N+ e8 J- D
二、实验内容 % ~4 j4 T' S, a
1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。 , q+ j% K) Q z) E4 t% Z- X* ]
2. 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,利用字符串处理函数,将其用空格连接在一起,并字母转换为大写,并将7.1替换为2016a。
0 ~2 g3 s# D8 j. L7 C3. 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.’,对该字符串做如下处理:
' j8 L6 @) H' Y" v& S(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。
( [; b8 i2 H6 S C% e5 ~(2)统计字符串中的数字和字母的个数。
9 @3 [& z, l) t: c0 G(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。 . j3 H- d D4 u& N3 j1 R. w0 h
4. 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。
! x* s5 }9 k0 U5. 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。
2 u* z3 y* }. {8 }( o! e/ s* Y; |+ j/ s- w
三、实验过程和结果
1 @7 Y( a3 B E0 o2 r1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。, R' G& G- t% m
% Z2 r- R: a6 y9 m+ S: r
- >> Str=['hopes, dreams, hold up, old up'];
- p=findstr(Str,'O')
- p =
- []
- >> length(p)
- ans =
- 05 W. x2 Z+ `/ m, l2 V2 }8 J ~) w
- r; ?& y8 B3 o& a! C# U! r
. T5 s' e" B u: {% t
2 . 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,,并字母转换为大写,并将7.1替换为2016a。
7 F3 m0 o; N6 i* f# _) u" s) f x
- >> 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, L* s" J3 y0 v9 _, F
' I: x0 o1 {0 C" ^! y8 }4 Y9 C: t' A+ _# L' i
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.’,对该字符串做如下处理: ; L7 \! i( X8 b6 p! r' b) @
(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。
8 r# _4 e: N0 ?/ {5 x0 W, k& k* J: B$ r2 ~" i
- >> 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/ N* p: s3 }& L
# g3 r6 J0 {2 `2 X7 Z* ]9 }5 @* d2 R( P8 C" I
(2)统计字符串中的数字和字母的个数。3 A5 ?# D& r( u4 q
$ V E# X3 i- S. a1 q2 r- >> 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
[7 d8 z3 ]* C) D1 C% e, h 8 t8 R0 B" r1 n+ I% Z
1 D4 V$ _( K7 A9 z; w1 D& W4 l
(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。# @7 _2 E9 [& ?' t
. |, m9 g4 [8 p6 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.';
- S=strrep(Str,' ','')
- k=find(S>='0'&S<='9');
- S(k)='';
- revch=S(end:-1:1)
- S =
- 1Theexistingresearchisaboutlocationtrackingeithercompletelyindoororaltogetheronopenair2byutilizingvarioussensorsandproceduresbasedoninter-networkingorinternetofthings.
- revch =
- .sgnihtfotenretnirognikrowten-retninodesabserudecorpdnasrosnessuoiravgnizilituybrianeponorehtegotlaroroodniyletelpmocrehtiegnikcartnoitacoltuobasihcraesergnitsixeehT
* G# P7 n7 r$ M) l/ q ! T2 p4 h# g. {
3 i! [9 [3 [1 L( F% o! {& G0 f, a4 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。( S+ T3 @: R4 w+ @1 i% O
L& f: I. H1 h' ?* A9 w5 B这里假设一个班有三名同学(不好意思,这个我暂时不会)6 S, j: n0 m! ?' T, Q- N) g7 e
( T7 K' [0 @6 }% ~2 A5 i- W- h I
5 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。
c6 d0 X5 P2 [5 T- V
% V: Y% [5 e. V: 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)
, w$ X4 y$ n( h( i6 j, n4 B5 W$ k
! f* y' E) P! A- ?8 z0 ]
) M5 W7 Z0 h+ }; z/ r1 v& g7 k, A% b/ v* ^0 K
四、实验总结和心得 5 h/ G F1 ~2 F& [
掌握了字符串的查找,连接,删除,倒置,替换等一系列操作
9 D( o O3 O: }% p. m! t掌握了结构数组和元胞数组的用法
" j8 R' G% X) y+ E! l; r1 \) |/ I' e* Q1 A" Z* n/ h" H
2 O- g/ U N& B' m; V# f. ~6 G
1 T6 H4 A2 M) `$ i& c; y O
/ k2 i5 n" U4 d/ Z0 G3 _+ a V9 Q0 @ |
|