MATLAB中是否有枚举类型?如果没有,有哪些替代方案?
答案 0 :(得分:41)
从R2010b开始,MATLAB支持枚举。
documentation的示例:
classdef Colors
properties
R = 0;
G = 0;
B = 0;
end
methods
function c = Colors(r, g, b)
c.R = r; c.G = g; c.B = b;
end
end
enumeration
Red (1, 0, 0)
Green (0, 1, 0)
Blue (0, 0, 1)
end
end
答案 1 :(得分:25)
您可以使用新式MATLAB类获得一些功能:
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是松散类型的,如果你使用整数,你可以做一些近似它的事情:
line1 = Colors.RED;
...
if Colors.BLUE == line1
end
在这种情况下,MATLAB“enums”接近C风格的枚举 - 替换整数的语法。
通过谨慎使用静态方法,您甚至可以使MATLAB枚举更接近Ada,但不幸的是语法更加笨拙。
答案 2 :(得分:18)
如果你想对Marc建议的类似做一些事情,你可以简单地用structure代表你的枚举类型而不是一个全新的类:
colors = struct('RED', 1, 'GREEN', 2, 'BLUE', 3);
一个好处是您可以通过两种不同的方式轻松访问结构。您可以使用字段名称直接指定字段:
a = colors.RED;
如果字符串中包含字段名称,则可以使用dynamic field names:
a = colors.('RED');
事实上,做Marc建议并创建一个全新的类以表示“枚举”对象有一些好处:
但是,如果您不需要那种复杂性并且只需要快速执行某些操作,那么结构可能是最简单,最直接的实现。它也适用于不使用最新OOP框架的旧版MATLAB。
答案 3 :(得分:8)
MATLAB R2009b中实际上有一个名为'enumeration'的关键字。它似乎没有文档,我不能说我知道如何使用它,但功能可能就在那里。
您可以在matlabroot\toolbox\distcomp\examples\+examples
classdef(Enumeration) DmatFileMode < int32
enumeration
ReadMode(0)
ReadCompatibilityMode(1)
WriteMode(2)
end
<snip>
end
答案 4 :(得分:7)
您也可以使用Matlab代码中的Java枚举类。用Java定义它们并将它们放在Matlab的javaclasspath上。
// Java class definition
package test;
public enum ColorEnum {
RED, GREEN, BLUE
}
您可以在M代码中按名称引用它们。
mycolor = test.ColorEnum.RED
if mycolor == test.ColorEnum.RED
disp('got red');
else
disp('got other color');
end
% Use ordinal() to get a primitive you can use in a switch statement
switch mycolor.ordinal
case test.ColorEnum.BLUE.ordinal
disp('blue');
otherwise
disp(sprintf('other color: %s', char(mycolor.toString())))
end
但它不会与其他类型进行比较。与string的比较有一个奇怪的返回大小。
>> test.ColorEnum.RED == 'GREEN'
ans =
0
>> test.ColorEnum.RED == 'RED'
ans =
1 1 1
答案 5 :(得分:5)
你可以制作一个行为类似于Java's old typesafe enum pattern的Matlab类。对Marc's solution的修改可以将它从C风格的typedef转换为更像Java风格的类型安全枚举。在此版本中,常量中的值是类型化的Color对象。
好处:
缺点:
总的来说,我不知道哪种方法更好。在实践中没有使用过。
classdef (Sealed) Color
%COLOR Example of Java-style typesafe enum for Matlab
properties (Constant)
RED = Color(1, 'RED');
GREEN = Color(2, 'GREEN');
BLUE = Color(3, 'BLUE');
end
properties (SetAccess=private)
% All these properties are immutable.
Code;
Name;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods (Access = private)
%private so that you can't instatiate directly
function out = Color(InCode, InName)
out.Code = InCode;
out.Name = InName;
end
end
methods (Static = true)
function needa(obj)
%NEEDA Asserts that obj must be a Color
if ~isa(obj, mfilename)
error('Input must be a %s; got a %s', mfilename, class(obj));
end
end
end
methods (Access = public)
function display(obj)
disp([inputname(1) ' =']);
disp(obj);
end
function disp(obj)
if isscalar(obj)
disp(sprintf('%s: %s (%d)', class(obj), obj.Name, obj.Code));
else
disp(sprintf('%s array: size %s', class(obj), mat2str(size(obj))));
end
end
function out = eq(a, b)
%EQ Basic "type-safe" eq
check_type_safety(a, b);
out = [a.Code] == [b.Code];
end
function [tf,loc] = ismember(a, b)
check_type_safety(a, b);
[tf,loc] = ismember([a.Code], [b.Code]);
end
function check_type_safety(varargin)
%CHECK_TYPE_SAFETY Check that all inputs are of this enum type
for i = 1:nargin
if ~isa(varargin{i}, mfilename)
error('Non-typesafe comparison of %s vs. %s', mfilename, class(varargin{i}));
end
end
end
end
end
这是一个锻炼它的功能。
function do_stuff_with_color(c)
%DO_STUFF_WITH_COLOR Demo use of the Color typesafe enum
Color.needa(c); % Make sure input was a color
if (c == Color.BLUE)
disp('color was blue');
else
disp('color was not blue');
end
% To work with switch statements, you have to explicitly pop the code out
switch c.Code
case Color.BLUE.Code
disp('blue');
otherwise
disp(sprintf('some other color: %s', c.Name));
end
使用示例:
>> Color.RED == Color.RED
ans =
1
>> Color.RED == 1
??? Error using ==> Color>Color.check_type_safety at 55
Non-typesafe comparison of Color vs. double
Error in ==> Color>Color.eq at 44
check_type_safety(a, b);
>> do_stuff_with_color(Color.BLUE)
color was blue
blue
>> do_stuff_with_color(Color.GREEN)
color was not blue
some other color: GREEN
>> do_stuff_with_color(1+1) % oops - passing the wrong type, should error
??? Error using ==> Color>Color.needa at 26
Input must be a Color; got a double
Error in ==> do_stuff_with_color at 4
Color.needa(c); % Make sure input was a color
>>
两种方法中的一个小怪癖:将常量放在“==”左侧以防止错误分配的C约定在这里没有多大帮助。在Matlab中,如果你不小心在LHS上使用“=”这个常量而不是错误,它只会创建一个名为Colors的新的局部结构变量,它将掩盖枚举类。
>> Colors.BLUE = 42
Colors =
BLUE: 42
>> Color.BLUE = 42
Color =
BLUE: 42
>> Color.RED
??? Reference to non-existent field 'RED'.
答案 6 :(得分:4)
如果您有权访问统计工具箱,则可以考虑使用categorical object。
答案 7 :(得分:3)
在尝试了这个页面上的其他建议之后,我找到了Andrew完全面向对象的方法。非常好 - 谢谢Andrew。
如果有人感兴趣,我做了(我认为是)一些改进。特别是,我删除了双重指定枚举对象名称的需要。现在使用反射和元类系统派生名称。此外,重写了eq()和ismember()函数,以便为枚举对象的矩阵返回正确形状的返回值。最后,修改了check_type_safety()函数,使其与包目录(例如命名空间)兼容。
似乎工作得很好,但让我知道你的想法:
classdef (Sealed) Color
%COLOR Example of Java-style typesafe enum for Matlab
properties (Constant)
RED = Color(1);
GREEN = Color(2);
BLUE = Color(3);
end
methods (Access = private) % private so that you can''t instatiate directly
function out = Color(InCode)
out.Code = InCode;
end
end
% ============================================================================
% Everything from here down is completely boilerplate - no need to change anything.
% ============================================================================
properties (SetAccess=private) % All these properties are immutable.
Code;
end
properties (Dependent, SetAccess=private)
Name;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods
function out = eq(a, b) %EQ Basic "type-safe" eq
check_type_safety(a, b);
out = reshape([a.Code],size(a)) == reshape([b.Code],size(b));
end
function [tf,loc] = ismember(a, b)
check_type_safety(a, b);
[tf,loc] = ismember(reshape([a.Code],size(a)), [b.Code]);
end
function check_type_safety(varargin) %CHECK_TYPE_SAFETY Check that all inputs are of this enum type
theClass = class(varargin{1});
for ii = 2:nargin
if ~isa(varargin{ii}, theClass)
error('Non-typesafe comparison of %s vs. %s', theClass, class(varargin{ii}));
end
end
end
% Display stuff:
function display(obj)
disp([inputname(1) ' =']);
disp(obj);
end
function disp(obj)
if isscalar(obj)
fprintf('%s: %s (%d)\n', class(obj), obj.Name, obj.Code);
else
fprintf('%s array: size %s\n', class(obj), mat2str(size(obj)));
end
end
function name=get.Name(obj)
mc=metaclass(obj);
mp=mc.Properties;
for ii=1:length(mp)
if (mp{ii}.Constant && isequal(obj.(mp{ii}.Name).Code,obj.Code))
name = mp{ii}.Name;
return;
end;
end;
error('Unable to find a %s value of %d',class(obj),obj.Code);
end;
end
end
谢谢, 梅森
答案 8 :(得分:2)
Toys = {'Buzz', 'Woody', 'Rex', 'Hamm'};
Toys{3}
ans = 'Rex'
答案 9 :(得分:1)
如果您只需要传递给C#或.NET程序集的枚举类型, 您可以使用MATLAB 2010构建和传递枚举:
A = NET.addAssembly(MyName.dll)
% suppose you have enum called "MyAlerts" in your assembly
myvar = MyName.MyAlerts.('value_1');
您还可以在
查看MathWorks的官方答案// the enum "MyAlerts" in c# will look something like this
public enum MyAlerts
{
value_1 = 0,
value_2 = 1,
MyAlerts_Count = 2,
}