我如何在Matlab中定义结构 [英] How do i define a structure in Matlab
问题描述
我知道可以通过多种方式定义结构,例如:
I know that a structure can be defined by in several ways such as:
向变量添加字段
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屋!