简体   繁体   English

Octave 是否支持像 Matlab 一样的枚举?

[英]Does Octave support enumeration like Matlab?

Does Octave support enumeration like Matlab? Octave 是否支持像 Matlab 一样的枚举
I haven't found any info about it.我还没有找到任何关于它的信息。

We can create an enumeration class by adding an enumeration block to a class definition.我们可以通过向类定义添加枚举块来创建枚举类。 For example, the WeekDays class enumerates a set of days of the week (from Matlab doc).例如, WeekDays 类枚举一周中的一组天数(来自 Matlab doc)。

%file WeekDays.m
classdef WeekDays
   enumeration
      Monday, Tuesday, Wednesday, Thursday, Friday
   end
end

And it work well in Matlab and I access enum values as它在 Matlab 中运行良好,我访问枚举值作为

x = WeekDays.Tuesday;

but Octave doesn't compile this line, despite file WeekDays.m is compiled by Octave without errors.但是Octave没有编译这一行,尽管文件 WeekDays.m 是由 Octave 编译的,没有错误。

I believe that in Octave version 4.0 there is experimental support for classdef -based object-oriented code, including enumeration blocks.我相信在 Octave 4.0 版中有对基于classdef的面向对象代码的实验性支持,包括enumeration块。

Edit: looks like I was wrong, and enumerations are not yet supported, as indicated in the comment below from @carandraug (who I believe is an Octave developer, so probably knows better then me).编辑:看起来我错了,枚举尚不受支持,如下面来自@carandraug 的评论所示(我认为他是 Octave 开发人员,所以可能比我更了解)。

The code example above still doesn't compile, in Octave 5.1.0 on a Mac.在 Mac 上的 Octave 5.1.0 中,上面的代码示例仍然无法编译。 It gives the error:它给出了错误:

octave:1> enumeration warning: the 'enumeration' function is not yet implemented in Octave Octave:1> 枚举警告:“枚举”功能尚未在 Octave 中实现

Progress on enumeration is tracked here: https://savannah.gnu.org/bugs/?44582在此处跟踪枚举的进展: https : //savannah.gnu.org/bugs/?44582

You can use static functions as a partial workaround:您可以使用静态函数作为部分解决方法:

classdef WeekDays
    methods (Static = true)
        function [out] = Monday()
            out = "Monday";
        end

        function [out] = Tuesday()
            out = "Tuesday";
        end

        function [out] = Wednesday()
            out = "Wednesday";
        end

        function [out] = Thursday()
            out = "Thursday";
        end

        function [out] = Friday()
            out = "Friday";
        end
    end
end

Similarly, you can also create enumerated objects;同样,您也可以创建枚举对象; I've uploaded template to google drive: https://drive.google.com/open?id=1-HftS5pdzE-oTmaC0kbnYcAcyAsaEcX6我已将模板上传到谷歌驱动器: https : //drive.google.com/open?id=1-HftS5pdzE-oTmaC0kbnYcAcyAsaEcX6

In the interest of completeness, here's an octave-style OOP 'enumerator' example:为了完整起见,这里有一个八度风格的 OOP 'enumerator' 示例:

% @Weekdays/Weekdays.m
function Obj = Weekdays( Weekday )
  Obj.enumeration = {'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'};
  if nargin == 0;   error( "Weekday cannot be initialized empty; choose from 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', or 'Sunday'."); end
  if ~ismember( Weekday, Obj.enumeration );   error( sprintf( "%s is not a valid Weekday. Choose from 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', or 'Sunday'.", Weekday ) ); end
  Obj.value = Weekday;
  Obj = class( Obj, 'Weekdays' );
end

% @Weekdays/display.m
function display(Obj);   display( get( Obj ) );   end

% @Weekdays/get.m
function Out = get( Obj );   Out = struct(Obj).value;   end

% @Weekdays/set.m
function Obj = set( Obj );
  error( "Enum objects have no public properties that can be set");
end

PS.附注。 if you really must have dot-syntax access, I suppose you can overload subsref accordingly too.如果你真的必须有点语法访问,我想你也可以相应地重载 subsref 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM