如何在 MATLAB 中创建枚举类型? [英] How do I create enumerated types in MATLAB?

查看:62
本文介绍了如何在 MATLAB 中创建枚举类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MATLAB 中有枚举类型吗?如果没有,有哪些替代方案?

Are there enumerated types in MATLAB? If not, what are the alternatives?

推荐答案

您可以通过新型 MATLAB 类获得一些功能:

You can get some of the functionality with new-style MATLAB classes:

classdef (Sealed) Colors
    properties (Constant)
        RED = 1;
        GREEN = 2;
        BLUE = 3;
    end

    methods (Access = private)    % private so that you cant instantiate
        function out = Colors
        end
    end
end

这不是一个真正的类型,但由于 MATLAB 是松散类型的,如果你使用整数,你可以做一些近似它的事情:

This isn't really a type, but since MATLAB is loosely typed, if you use integers, you can do things that approximate it:

line1 = Colors.RED;
...
if Colors.BLUE == line1
end

在这种情况下,MATLAB 的枚举"接近于 C 风格的枚举——用整数代替语法.

In this case, MATLAB "enums" are close to C-style enums - substitute syntax for integers.

通过谨慎使用静态方法,您甚至可以使 MATLAB 枚举在复杂性上接近 Ada,但不幸的是语法更笨拙.

With the careful use of static methods, you can even make MATLAB enums approach Ada's in sophistication, but unfortunately with clumsier syntax.

这篇关于如何在 MATLAB 中创建枚举类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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