matlab 结构体字段,MATLAB基本操作(四):结构体struct&元胞数组cell | 学步园

>> student(1).name='Tom';

student(1).age=20;

student(1).sex='male';

>> student(2).name='rose';

student(2).age=21;

student(2).sex='female';

>> student

student =

1x2 struct array with fields:

name

age

sex

>> student(1)

ans =

name: 'Tom'

age: 20

sex: 'male'

>> student(2)

ans =

name: 'rose'

age: 21

sex: 'female'

2)用关键字struct创建

>> student=struct('name',{'Tom','rose'},'age',{20,21});

>> student

student =

1x2 struct array with fields:

name

age

>> student(1)

ans =

name: 'Tom'

age: 20

>> student(2)

ans =

name: 'rose'

age: 21

3)要增加字段怎么办?直接在结构体名后加 .字段名

student(1).id=100;

4)要删除字段呢?使用函数rmfield(,)

student=rmfield(student,'id');

5)几个函数

fieldnames(student); %返回字段名

isfield(student,'age');

isstruct(student);

struct2cell(student);

二,元胞数组

它与数组的区别是:每个元素可以是不同类型的,可是不同大小的矩阵,也可以是字符串,结构体等

使用元胞数组要区分(),{}的区别。

1)创建元胞数组

>> c(1,1)={[1,2;3 4]};

>> c(1,2)={'tom is a sb'};

>> c(2,1)={[1,2,3,4,5,6]};

>> c(2,2)={struct('name','rose','age',21)};

>> c

c =

[2x2 double] 'tom is a sb'

[1x6 double] [1x1 struct]

>> c(1,1)

ans =

[2x2 double]

>> c{1,1}

ans =

1 2

3 4

>>

{ }:可以返回一个元胞中的具体数据;

() :返回该位置的数组类型信息

>> B(3,3)={'hello'};

>> B

B =

[] [] []

[] [] []

[] [] 'hello'

>>

创建一个3*3的元胞数组,并组在(3,3)位置的赋值

2)几个函数

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值