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

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

[复制链接]

该用户从未签到

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

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 @
  • TA的每日心情

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

    [LV.1]初来乍到

    2#
    发表于 2019-12-31 19:03 | 只看该作者
    学习了:Matlab字符串、单元数组和结构体

    该用户从未签到

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

    本版积分规则

    关闭

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

    EDA365公众号

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

    GMT+8, 2025-11-1 20:25 , Processed in 0.171875 second(s), 26 queries , Gzip On.

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

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

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