...
The MATLAB mapping for Fruit
is shown below:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
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:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
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:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
val = int32(Fruit.Pear);
assert(val == 1); |
To convert an integer into its equivalent enumerator, call the ice_getValue
function:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
f = Fruit.ice_getValue(2);
assert(f == Fruit.Orange); |
...
The generated code changes accordingly:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
classdef Fruit < int32
enumeration
Apple (0)
Pear (3)
Orange (4)
end
...
end |
...