1、结构体
(1)使用赋值方法创建结构体
%% 使用赋值方法创建结构体
person(1).name = '张三';
person(1).weight = 66;
person(1).length = 200;
person(1).width = 30;
person(2).name = '李四';
person(2).weight = 70;
person(2).lengh = 100;
person(2).width = 40;
(2)使用struct创建结构体
%% 使用struct创建结构体
person = struct('name','张三','weight',66,'lengh',100,'width',200);
person = struct('name',{'张三','李四'},'weight',{66,70},'lengh',{100,120},'width',{200,150});
person(1)
person(2)
ans =
包含以下字段的 struct:
name: '张三'
weight: 66
lengh: 100
width: 200
ans =
包含以下字段的 struct:
name: '李四'
weight: 70
lengh: 120
width: 150
(3)访问结构体内部元素
获取:结构体名称.字段名
赋值:结构体名称.字段名 = 新值
%% 访问结构体内部元素
person = struct('name',{'张三','李四'},'weight',{66,70},'lengh',{100,120},'width',{200,150});
person(1).name % '张三'
person(1).width = 222
person(1)
% name: '张三'
% weight: 66
% lengh: 100