结构体
基本数据
- name-------------- 张三
- weight------------ 66kg
- length------------ 200km
- width------------- 30km
一个数组内的数据类型都相同
结构体
- person.weight=88
- person.length=288
- person.width=208
使用赋值语句创建结构体
输入:
preson.name='张三';
person.weight=80;
person.length=200;
person.width=40;
person
输出:
preson =
weight: 80
length: 200
width: 40
name: '张三'
输入:
person(1).name='张三';
person(1).weight=80;
person(1).length=200;
person(1).width=40;
person(2).name='李四';
person(2).weight=70;
person(2).length=100;
person(2).width=40;
person
person(1)
person(2)
输出:
person =
1x2 struct array with fields:
weight
length
width
name
ans =
weight: 80
length: 200
width: 40
name: '张三'
ans =
weight: 70
length: 100
width: 40
name: '李四'
struct函数
语法:
struct(域名1,属性值,域名2,属性值,…)
输入:
person=struct('name','张三','weight',100,'height',180);
person
输出:
person =
name: '张三'
weight: 100
height: 180
输入:
person=struct('name',{'张三','李四'},'weight',{100,80},'height',{180,170});
person
person(1)
person(2)
输出:
person =
1x2 struct array with fields:
name
weight
height
ans =
name: '张三'
weight: 100
height: 180
ans =
name: '李四'
weight: 80
height: 170
访问结构体内部元素
语法格式:
结构体名称
结构体名称.字段名
结构体名称.字段名=新值