如何在 MATLAB 中创建枚举类型?
MATLAB 中有枚举类型吗?如果没有,还有哪些替代方案?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
MATLAB 中有枚举类型吗?如果没有,还有哪些替代方案?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
从 R2010b 开始,MATLAB 支持枚举。
文档中的示例:
Starting from R2010b, MATLAB supports enumerations.
Example from the documentation:
您可以使用新型 MATLAB 类获得一些功能:
这并不是真正的类型,但由于 MATLAB 是松散类型的,如果您使用整数,您可以执行近似它的操作:
在本例中,MATLAB“枚举”接近 C 风格的枚举 - 整数的替代语法。
通过仔细使用静态方法,您甚至可以使 MATLAB 枚举在复杂性上接近 Ada,但不幸的是语法更加笨拙。
You can get some of the functionality with new-style MATLAB classes:
This isn't really a type, but since MATLAB is loosely typed, if you use integers, you can do things that approximate it:
In this case, MATLAB "enums" are close to C-style enums - substitute syntax for integers.
With the careful use of static methods, you can even make MATLAB enums approach Ada's in sophistication, but unfortunately with clumsier syntax.
如果您想做一些类似于 Marc 建议的事情,您只需制作一个 结构 来表示您的枚举类型而不是一个全新的类:
一个好处是您可以通过两种不同的方式轻松访问结构。您可以直接使用字段名称指定字段:
或者可以使用 动态字段名称(如果您在字符串中包含字段名称):
事实上,按照 Marc 建议并创建一个全新的类来表示“枚举”对象有一些好处
但是,如果您不需要这种复杂性而只需要快速执行某些操作,那么结构可能是最简单、最直接的实现。它还可以与不使用最新 OOP 框架的旧版本 MATLAB 配合使用。
If you want to do something similar to what Marc suggested, you could simply make a structure to represent your enumerated types instead of a whole new class:
One benefit is that you can easily access structures in two different ways. You can specify a field directly using the field name:
or you can use dynamic field names if you have the field name in a string:
In truth, there are a few benefits to doing what Marc suggested and creating a whole new class to represent an "enum" object:
However, if you don't need that sort of complexity and just need to do something quick, a structure is likely the easiest and most straight-forward implementation. It will also work with older versions of MATLAB that don't use the newest OOP framework.
您还可以使用 Matlab 代码中的 Java 枚举类。在 Java 中定义它们并将它们放在 Matlab 的 javaclasspath 中。
您可以在 M 代码中通过名称引用它们。
但它不会与其他类型进行比较。与字符串的比较有一个奇怪的返回大小。
You could also use Java enum classes from your Matlab code. Define them in Java and put them on your Matlab's javaclasspath.
You can reference them by name in M-code.
It won't catch comparisons to other types, though. And comparison to string has an odd return size.
MATLAB R2009b 中实际上有一个名为'enumeration' 的关键字。它似乎没有记录,我不能说我知道如何使用它,但功能可能是存在的。
您可以在
matlabroot\toolbox\distcomp\examples\+examples
中找到它There is actually a keyword in MATLAB R2009b called 'enumeration'. It seems to be undocumented, and I cannot say I know how to use it, but the functionality is probably there.
You can find it in
matlabroot\toolbox\distcomp\examples\+examples
你可以创建一个Matlab类,其行为类似于Java的旧类类型安全枚举模式。对Marc的解决方案进行修改可以接受它从 C 风格的 typedef 到更像 Java 风格的类型安全枚举。在此版本中,常量中的值是类型化的 Color 对象。
优点:
缺点:
总的来说,我不知道哪种方法更好。实践中也没用过。
这是一个练习它的函数。
使用示例:
这两种方法都有一个小问题:将常量放在“==”左侧以防止错误赋值的 C 约定在这里没有多大帮助。在 Matlab 中,如果您不小心在 LHS 上使用“=”与此常量,它只会创建一个名为 Colors 的新局部结构变量,而不是出现错误,并且会屏蔽枚举类。
You could make a Matlab class that behaves like a Java's old typesafe enum pattern. A modification of Marc's solution could take it from C-style typedefs to more like Java-style typesafe enums. In this version, the values in the constants are typed Color objects.
The upsides:
Downsides:
On the whole, I don't know which approach is better. Haven't used either in practice.
Here's a function to exercise it.
Example of use:
A minor quirk in both approaches: the C convention of putting the constant on the left hand of the "==" to prevent bad assignment doesn't help as much here. In Matlab, if you accidentally use "=" with this constant on the LHS, instead of an error, it'll just create a new local struct variable named Colors, and it will mask the enum class.
如果您有权访问统计工具箱,则可以考虑使用分类对象。
If you have access to the Statistics Toolbox, you might consider using a categorical object.
在尝试了本页上的其他建议后,我采用了 Andrew 的完全面向对象的方法。非常好 - 谢谢安德鲁。
不过,如果有人感兴趣,我做了(我认为是)一些改进。特别是,我不再需要双重指定枚举对象的名称。现在使用反射和元类系统派生名称。此外,重写了 eq() 和 ismember() 函数,以返回枚举对象矩阵的正确形状的返回值。最后,修改了 check_type_safety() 函数以使其与包目录(例如命名空间)兼容。
看起来效果很好,但请告诉我你的想法:
谢谢,
石匠
After trying out the other suggestions on this page, I landed on Andrew's fully object-oriented approach. Very nice - thanks Andrew.
In case anyone is interested, however, I made (what I think are) some improvements. In particular, I removed the need to double-specify the name of the enum object. The names are now derived using reflection and the metaclass system. Further, the eq() and ismember() functions were re-written to give back properly-shaped return values for matrices of enum objects. And finally, the check_type_safety() function was modified to make it compatible with package directories (e.g. namespaces).
Seems to work nicely, but let me know what you think:
Thanks,
Mason
如果您需要枚举类型只是为了传递给 C# 或 .NET 程序集,
您可以使用 MATLAB 2010 构造并传递枚举:
您还可以查看官方 MathWorks 答案:
If you need the enumerated types just for passing to C# or .NET assembly,
you can construct and pass the enums with MATLAB 2010:
you can also check the official MathWorks answer at