简体   繁体   中英

Does Octave support enumeration like Matlab?

Does Octave support enumeration like 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).

%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

x = WeekDays.Tuesday;

but Octave doesn't compile this line, despite file WeekDays.m is compiled by Octave without errors.

I believe that in Octave version 4.0 there is experimental support for classdef -based object-oriented code, including enumeration blocks.

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).

The code example above still doesn't compile, in Octave 5.1.0 on a Mac. It gives the error:

octave:1> enumeration warning: the 'enumeration' function is not yet implemented in Octave

Progress on enumeration is tracked here: 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

In the interest of completeness, here's an octave-style OOP 'enumerator' example:

% @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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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