找回密码
 注册
关于网站域名变更的通知
查看: 1061|回复: 2
打印 上一主题 下一主题

Matlab字符串、单元数组和结构体

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-12-31 09:52 | 只看该作者 |只看大图 回帖奖励 |正序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x

5 m4 q8 p+ A  v一、实验任务和目的
! N  D1 @1 K2 E1. 掌握Matlab的字符串常用函数及其操作方法。
3 z, x+ |; g: |* C2. 掌握Matlab的结构体的基本操作方法。 5 Z, i/ u- T; C# j) Z
3. 掌握Matlab的元胞数组的基本操作方法。
+ S# a! ?* f  W/ p二、实验内容 ; z( w4 s2 v2 j" p
1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。 & P# \" W$ ?, Q# o: Y3 W
2. 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,利用字符串处理函数,将其用空格连接在一起,并字母转换为大写,并将7.1替换为2016a。 ' o+ K) |7 T3 m3 _) {  }' C
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.’,对该字符串做如下处理:
5 }8 ^# m2 N" ?(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。
9 ^: H  y( N# c; b5 s' ?(2)统计字符串中的数字和字母的个数。
' C  l5 D- _, }; _3 y(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。
8 I% Y7 L* c+ @4. 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。 4 B6 s0 ^) }5 _% A; @
5. 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。
6 R( T9 x2 I7 H! I1 l  f/ Z( E8 A5 r0 V$ w' Z7 e! b
三、实验过程和结果
2 N9 m+ p: x/ [1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。
# a( _5 V" e6 {9 j; ~5 s  `$ e4 k* ^, f) L/ ~
  • >> Str=['hopes, dreams, hold up, old up'];
  • p=findstr(Str,'O')
  • p =
  •      []
  • >> length(p)
  • ans =
  •      0" K  w# H1 @; q6 Y' S  n! w

. M9 t; w7 i7 U* f- G  N/ Q7 ?" T6 m- R. I0 g
2 . 现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,,并字母转换为大写,并将7.1替换为2016a。
) O1 i/ z$ S/ m6 p& N" m: V/ u- i) y- W+ ~6 O
  • >> 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
    ' B# D2 s0 R$ q: n2 h# s4 S1 Y4 n$ G
& ], H8 C3 j4 N" ?4 {  R- E

7 d, ^+ U+ a3 ~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 L: S/ d, m" L  a
(1)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。
- o* V; b: y. i( l( b! K( R0 f' x2 c' 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=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.
    & s7 i6 i2 c1 v" B! O7 i, u, E
3 @$ L. D: X, y' r  K$ ^* a) A* W

0 h) G5 l2 c7 _* A1 i2 f; I(2)统计字符串中的数字和字母的个数。, S* b: p# J* z/ @4 J

8 m! Q! G. e- l  w: 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=find(Str>='0'&Str<='9');
  • >> m=find(Str>='a'&Str<='z');
  • >> length(k)
  • ans =
  •      2
  • >> length(m)
  • ans =
  •    162. U" O1 Z3 K1 y1 M( q7 [
: \$ B: y% b* _8 _$ y
* J2 V3 C- q  S5 G2 D
(3)将字符串中间的空格和数字删除,所有字母倒过来重新排序。6 b# t2 _2 D6 ^9 r! G* S

( N- @: |) B  Y% x2 I9 k$ u
  • >> 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* C' s6 D/ t' X! g7 T

1 w, i% V/ W5 N7 }
4 d9 B% A- M' l( r+ i4 创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。
) Z5 j$ m% C, x3 l2 H: k! e  U: t" @" J0 n# `: H
这里假设一个班有三名同学(不好意思,这个我暂时不会)
7 ]! n/ ~; F9 f8 [4 o2 i& `
% Q" F/ y1 k% U( R5 创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。
- P- \1 m: _( E5 m
! ~$ x0 b# w/ R
  • >> 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)! C1 _  k8 l3 d6 E
  R" J# J' v9 o0 n- n

( G) e: E/ i: a, Y% E
, Z2 s" J8 x( N3 [/ r四、实验总结和心得
4 E2 P! U. h9 z1 n) b掌握了字符串的查找,连接,删除,倒置,替换等一系列操作 ( Q+ o  o4 l- ]! P$ z% ~' l
掌握了结构数组和元胞数组的用法
, \- f9 `. Y3 b9 H3 E3 [: |; `2 k
4 s: N4 {8 c2 t8 c( G2 v' J/ Z1 d" d

  }) n) p2 ?  Q6 y9 K- G7 B! u+ @
! v$ I7 f  Z) _' w% G  V7 k. q5 H

该用户从未签到

3#
发表于 2020-1-2 18:42 | 只看该作者
学习了:Matlab字符串、单元数组和结构体
  • TA的每日心情

    2019-11-29 15:37
  • 签到天数: 1 天

    [LV.1]初来乍到

    2#
    发表于 2019-12-31 19:03 | 只看该作者
    学习了:Matlab字符串、单元数组和结构体
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    关闭

    推荐内容上一条 /1 下一条

    EDA365公众号

    关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

    GMT+8, 2025-11-23 22:43 , Processed in 0.156250 second(s), 27 queries , Gzip On.

    深圳市墨知创新科技有限公司

    地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

    快速回复 返回顶部 返回列表