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 @   叕叒双又  阅读(442)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-11-25 数据库上手避坑之--探索数据库
2017-11-25 《Linux命令行与shell脚本编程大全 第3版》创建实用的脚本---11
2017-11-25 《Linux命令行与shell脚本编程大全 第3版》创建实用的脚本---10
2017-11-25 《Linux命令行与shell脚本编程大全 第3版》创建实用的脚本---07
2017-11-25 《Linux命令行与shell脚本编程大全 第3版》创建实用的脚本---06
2017-11-25 《Linux命令行与shell脚本编程大全 第3版》创建实用的脚本---05
2017-11-25 《Linux命令行与shell脚本编程大全 第3版》创建实用的脚本---03
点击右上角即可分享
微信分享提示