>> 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)几个函数