《matlab》 字符串、单元数组和结构

一、实验任务和目的

1. 掌握Matlab的字符串常用函数及其操作方法。

2. 掌握Matlab的结构体的基本操作方法。

3. 掌握Matlab的元胞数组的基本操作方法。

二、实验内容

1. 字符串数组Str=[‘hopes, dreams, hold up, old up’],查找’O’出现的次数和位置。

>> Str=['hopes, dreams, hold up, old up'];

>> x=findstr(Str,'o')

x =

     2    17    25

>> length(x)

ans =

     3

2.现有三个字符串变量s1=“i”,s2=“love”,s3=“matlab7.1”,利用字符串处理函数,将其用空格连接在一起,并字母转换为大写,并将7.1替换为2016a。

>> 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 =

I LOVE MATLAB7.1

>> strrep(s4,'7.1','2016a')

ans =

I LOVE MATLAB2016a

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)判断字符串中每个单词的首字母是否大写,若不是则将其修改为大写,其他字母为小写。

>> 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.

4.统计字符串中的数字和字母的个数。

>> 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

  5.将字符串中间的空格和数字删除,所有字母倒过来重新排序。

>> 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.';

>> x=strrep(Str,' ','');

>> k=find(x>='0' & x<='9');

>> x(k)='';

>> j=x(end:-1:1)

j =

.sgnihtfotenretnirognikrowten-retninodesabserudecorpdnasrosnessuoiravgnizilituybrianeponorehtegotlaroroodniyletelpmocrehtiegnikcartnoitacoltuobasihcraesergnitsixeehT

  6.创建一个结构体,用于统计学生的情况,包括学生的姓名、学号、各科成绩等。然后使用该结构体对一个班级的学生的成绩进行管理,如计算总分、平均分、排列名次等。

>> person=struct('name',{'wangming','zhangpeng'},'id',{54200,54211},'grade',{80,90})

person =

1x2 struct array with fields:

    name

    id

    grade

>> person(1)

ans =

     name: 'wangming'

       id: 54200

    grade: 80

>> person(2)

ans =

     name: 'zhangpeng'

       id: 54211

       grade: 90

7.创建一个2X2的元胞数组,第1、2个元素为字符串,第3元素为整型,第4元素为双精度类型,并将其用图形表示。


Maybe you will meet some problems and challenges,but l believe you will not be defeated by it

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zzuli 山之涯海之角

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值