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

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

问题描述

在MATLAB中有枚举类型吗?

解决方案

您可以使用新的MATLAB类获取一些功能:

  classdef(Sealed)颜色
属性(常量)
RED = 1;
GREEN = 2;
BLUE = 3;
end

方法(Access = private)%private,以便您无法实例化
函数out = Colors
end
end
end

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

  line1 = Colors.RED; 
...
如果Colors.BLUE == line1
end

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



仔细使用静态方法,甚至可以MATLAB枚举方法Ada的复杂性,但不幸的是使用笨拙的语法。


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

解决方案

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

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

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.

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

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