Reputation: 3009
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.
Upvotes: 5
Views: 3415
Reputation: 22245
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.
Upvotes: 0
Reputation: 1
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
Upvotes: 0
Reputation: 984
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
Upvotes: 0
Reputation: 24127
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).
Upvotes: 2