matlab-结构数组

>> student.name='张三'

student =

    name: '张三'

>> student.age=22

student =

    name: '张三'
     age: 22

>> student.email='dd@11.net'

student =

     name: '张三'
      age: 22
    email: 'dd@11.net'

>> student(2).name='硅'

student =

1x2 struct array with fields:
    name
    age
    email

>> student(2).age=25

student =

1x2 struct array with fields:
    name
    age
    email

>> student(2).email='ssss@111.net'

student =

1x2 struct array with fields:
    name
    age
    email

查询结构数组 
>> student

student =

1x2 struct array with fields:
    name
    age
    email

>> fieldnames(student)

ans =

    'name'
    'age'
    'email'

>> student(1)

ans =

     name: '张三'
      age: 22
    email: 'dd@11.net'

>> student(2)

ans =

     name: '硅'
      age: 25
    email: 'ssss@111.net'

>>

得到某字段内容

>> getfield(student(1),'name')

ans =

张三

>> getfield(student(1),'age')

ans =

    22

>>

 

某个字段是否属于这个数组结构

>> isfield(student,'age')

ans =

     1

>> isfield(student,'ages')

ans =

     0

>>

设置字段内容

>> setfield(student(1),'age',26)

ans =

     name: '张三'
      age: 26
    email: 'dd@11.net'

删除

 
>> rmfield(student,'email')

ans =

1x2 struct array with fields:
    name
    age

新建

>> myst=struct('f1','sss','f2','aaa')

myst =

    f1: 'sss'
    f2: 'aaa'

>>

 

>> student.name='aaa'

student =

    name: 'aaa'

>> zdname='name'

zdname =

name

>> student.(zdname)

ans =

aaa

>>

关于struct


>> mys=struct('name',{'张三','李四'},'age',{28,36})

mys =

1x2 struct array with fields:
    name
    age

>> mys(1)

ans =

    name: '张三'
     age: 28

>> mys(2)

ans =

    name: '李四'
     age: 36

>>

 

动态字段名

>> aaa='name'

aaa =

name

>> xx=mys.(aaa)

xx =

张三

>> mys.(aaa)

ans =

张三


ans =

李四

 
>> xx(1)

ans =

>> xx(2)

ans =

>>  

 结构数组大小


>> size(mys)

ans =

     1     2
>> size(mys(1).name)

ans =

     1     2

>>

 

删除字段

>> rmfield(mys,'name')

ans =

1x2 struct array with fields:
    age

>>

增加字段

>> mys(1).class='计数班'

mys =

1x2 struct array with fields:
    name
    age
    class

>> mys

mys =

1x2 struct array with fields:
    name
    age
    class

>>

你可能感兴趣的:(matlab)