matlab细胞数组类型建立及操作

细胞数组结构图:

细胞数组中每个细胞存储一种类型的MATLAB数组,此数组中的数据可以是任何一种MATLAB数据类型或用户自定义的类型,其大小也可以是任意的。相同数组的第二个细胞的类型与大小可以和第一个细胞完全不同。

建立细胞数组并输出结构图:

>> stu=cell(2);  %运用cell函数建立2×2细胞数组
>> stu{1,1}={'Xiao','Na'};
>> stu{1,2}={'20180101','20190102'};
>> stu{2,1}={'f','m'};
>> stu{2,2}={20,19};
>> cellplot(stu);  %显示细胞结构数组


细胞数组操作函数的基本使用:

>> stu_cell={'XiaoNa','20180412','M','20'};  %建立细胞数组
>> celldisp(stu_cell);  %显示细胞数组
 
stu_cell{1} =
 
XiaoNa
 
 
stu_cell{2} =
 
20180412
 
 
stu_cell{3} =
 
M
 
 
stu_cell{4} =
 
20
 
>> fields={'name','number','sex','age'};
>> stu_struct=cell2struct(stu_cell,fields,2);  %将细胞数组转换为结构体
>> stu_struct

stu_struct =

      name: 'XiaoNa'
    number: '20180412'
       sex: 'M'
       age: '20'

>> a=iscell(stu_cell);  %判断stu_cell是否是细胞数组
>> a

a =

     1

>> stu_t=struct('name',{'XiaoNa','WangHong'},'number',{'20180101','20180102'},'sex',{'f','m'},'age',{20,19});
>> stu_c=struct2cell(stu_t);  %将结构体转换为细胞数组
>> stu_c

stu_c(:,:,1) =

    'XiaoNa'
    '20180101'
    'f'
    [20]


stu_c(:,:,2) =

    'WangHong'
    '20180102'
    'm'
    [19]
 
>> c={[1] [2 3 4];[5; 9] [6 7 8; 10 11 12]};  %建立细胞数组

>> cellplot(c);  %显示细胞数组c结构图


>> M=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20];  %创建矩阵M
>> M

M =

     1     2     3     4     5
     6     7     8     9    10
    11    12    13    14    15
    16    17    18    19    20

>> C1=mat2cell(M,[2 2],[3 2]);  %将M拆分成细胞数组
>> C2=num2cell(M);  %将M转换成细胞数组
>> figure;
>> subplot(121);cellplot(C1);  %显示C1结构图

>> subplot(122);cellplot(C2);  %显示C2结构图



------五万之路,乃成神之路

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值