我如何在Matlab中定义一个结构 [英] How do i define a structure in Matlab

查看:139
本文介绍了我如何在Matlab中定义一个结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以通过以下几种方式定义一个结构:

I know that a structure can be defined by in several ways such as:


  1. 向变量添加字段



  1. Adding fields to a variable

p.color.red = .2;
p.color.green = .4;
p.color.blue = .7;


  • 通过赋值定义标量结构

  • Defining a scalar structure by assignment

    S = struct('a',  0, 'b',  1, 'c',  2);
    


  • 我想要做的是创建一个结构定义(如C)。我的最终目标是拥有一组可以遍历并执行测试的结构。有什么办法可以定义一个通用结构然后创建它的实例吗?我应该用其他的机制吗我知道MATLAB支持Java,应该使用类/接口吗?

    What I want to be able to do is create a structure definition (like C). My end goal is to have an array of structures that i can iterate through and perform testing on. Is there any way I can define a generic structure and then create instances of it? Should I use some other mechanism. I know MATLAB supports Java, should I use a class/interface?

    推荐答案

    与C不同,Matlab允许您添加或删除成员从一个结构体,当你走。这是一个约定,某些字段的结构可以与某些功能一起使用。

    Unlike C, Matlab allows you to add or remove members from a struct as you go. It's more of a convention that a struct with certain fields can be used with certain functions.

    所以是的,这些是创建结构的两种基本方法。当然,您可以随时在* .m文件中编写一个函数,为您创建一个包含某些字段的结构。或者,如果您想获得更多OO,请参阅此处

    So yes, those are the two basic ways to create structures. Of course, you can always write a function in a *.m file which creates a structure with certain fields for you. Or if you want to get more OO, see here

    这篇关于我如何在Matlab中定义一个结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆