matlab的结构体

结构体(struct)是一种能够将不同类型和大小的数据组合在一起的容器。它允许将数据分配给命名的字段(fields),每个字段可以存储不同的数据类型,如数值、字符串、数组、矩阵等。以下是 MATLAB 结构体的创建、访问和操作的基本用法。

1 创建结构体:

Student=struct('name','zhangsan','age',20,'sex','femal')

octave:11> Student=struct('name','zhangsan','age',20,'sex','f')
Student =

  scalar structure containing the fields:

    name = zhangsan
    age = 20
    sex = f

2 结构体字段的访问,使用.运算符

octave:12> Student.name
ans = zhangsan
octave:13> Student.age
ans = 20
octave:14> Student.sex
ans = f

3 结构体字段的修改,使用.运算符获取,使用=修改

octave:15> Student.name='wangwu';
octave:16> disp(Student)
    name = wangwu
    age = 20
    sex = f

4 结构体添加新字段,使用.运算符和=运算符

Student.newField='info'
Student =

  scalar structure containing the fields:

    name = wangwu
    age = 20
    sex = f
    newField = info
octave:43> Student.info='other'
Student =

  scalar structure containing the fields:

    name = wangwu
    age = 20
    sex = f
    newField = info
    info = other

5 结构体删除字段,使用rmfield

Student=rmfield(Student,'newField')
Student =

  scalar structure containing the fields:

    name = wangwu
    age = 20
    sex = f
    info = other

记得一定要覆盖下,否则是没有变化的,只是显示上变化了而已

6 结构体的嵌套

Student.info=struct('city','guizhou','zip','10001')
Student =

  scalar structure containing the fields:

    name = wangwu
    age = 20
    sex = f
    info =

      scalar structure containing the fields:

        city = guizhou
        zip = 10001

访问需要加上其前缀:

octave:48> Student.info.city
ans = guizhou
octave:49> Student.info.zip
ans = 10001

7 结构体的数组

Stu(1)=struct('name','zhangsan','age',20,'info',struct('city','guizhou','zip',10001))
Stu =

  scalar structure containing the fields:

    name = zhangsan
    age = 20
    info =

      scalar structure containing the fields:

        city = guizhou
        zip = 10001


octave:51> Stu(2)=struct('name','lisi','age',29,'info',struct('city','guangzhou','zip',10002))
Stu =

  1x2 struct array containing the fields:

    name
    age
    info

octave:52> Stu(3)=struct('name','wangwu','age',19,'info',struct('city','zhengzhou','zip',10004))
Stu =

  1x3 struct array containing the fields:

    name
    age
    info

访问和单独的访问方式也是一样的,

octave:53> Stu(1).name
ans = zhangsan
octave:54> Stu(1).info.city
ans = guizhou

8 结构体遍历,使用 fieldnames 函数获取结构体中的字段名,并通过循环遍历:

octave:56> fields=fieldnames(Stu)
fields =
{
  [1,1] = name
  [2,1] = age
  [3,1] = info
}
octave:57> for i=1:numel(fields)
> fieldName=fields{i};
> fieldValue=Stu.(fieldName);
> fprintf('%s:',fieldName);
> disp(fieldValue);
> end
name:zhangsan
age:20
info:    city = guizhou
    zip = 10001
octave:58> fields
fields =
{
  [1,1] = name
  [2,1] = age
  [3,1] = info
}

就记录到这里吧

a \stackrel{b} {\longrightarrow} c  \\a \stackrel{b} {\longleftarrow} c  \\\vec{A}  \\\mathop{n}\limits ^{\rightarrow}  \\\mathop{n}\limits ^{\leftarrow}  \\\widehat{A}\\\hat{A} \\\widecheck{A} \\\check{A}  \\\widetilde{A}  \\\tilde{A} \\\overline{A}  \\\underline{A}  \\\dot{A} \\\ddot{A} \\\dddot{A} \\\ddddot{A}  \\\mathring{A}  \\\breve{A}  \\\grave{A}  \\\acute{A}  \\\bar{A}
posted @ 2024-11-25 16:39  叕叒双又  阅读(138)  评论(0编辑  收藏  举报