A Slice enumeration maps to the corresponding enumeration in MATLAB. For example:
enum Fruit { Apple, Pear, Orange }
The MATLAB mapping for Fruit
is shown below:
classdef Fruit < int32 enumeration Apple (0) Pear (1) Orange (2) end methods(Static) ... function r = ice_getValue(v) switch v case 0 r = Fruit.Apple; case 1 r = Fruit.Pear; case 2 r = Fruit.Orange; otherwise throw(Ice.MarshalException(...)); end end end end
Given the above definitions, we can use enumerated values as follows:
f1 = Fruit.Apple; f2 = Fruit.Orange; if f1 == Fruit.Apple % Compare with constant % ... end if f1 == f2 % Compare two enums % ... end switch f2 % Switch on enum case Fruit.Apple % ... case Fruit.Pear % ... case Fruit.Orange % ... end
You can obtain the ordinal value of an enumerator using the int32
function:
val = int32(Fruit.Pear); assert(val == 1);
To convert an integer into its equivalent enumerator, call the ice_getValue
function:
f = Fruit.ice_getValue(2); assert(f == Fruit.Orange);
This function raises an exception if the given integer does not match any of the enumerators.
Note that the generated class contains a number of other members, which we have not shown. These members are internal to the Ice run time and you must not use them in your application code (because they may change from release to release).
The Fruit
definition above shows the ordinal values assigned by default to the enumerators. Suppose we modify the definition to include a custom enumerator value:
enum Fruit { Apple, Pear = 3, Orange }
The generated code changes accordingly:
classdef Fruit < int32 enumeration Apple (0) Pear (3) Orange (4) end ... end