.NET Enumerations in the MATLAB Workspace

Using the NetDocEnum Example Assembly

Some of the examples in this topic use the System.DayOfWeek enumeration, which is part of the .NET Framework. The C# example NetDocEnum.cs, in the matlabroot/extern/examples/NET/NetSample folder, defines enumerations used in other examples. To see the code, open the fileopen the file in MATLAB® Editor. To run the examples, build the NetDocEnum assembly as described in Build a .NET Application for MATLAB Examples.

If the NetDocEnum assembly is in your c:\work folder, load the file:

dllPath = fullfile('c:','work','NetDocEnum.dll');
asm = NET.addAssembly(dllPath);
asm.Enums
ans = 
    'NetDocEnum.MyDays'
    'NetDocEnum.Range'

Work with Members of a .NET Enumeration

To display the member names of an enumeration, use the MATLAB enumeration function. For example, to list the member names of the System.DayOfWeek enumeration, type:

enumeration('System.DayOfWeek')
Enumeration members for class 'System.DayOfWeek':
    Sunday
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday

You cannot use the enumeration command to return arrays of .NET enumeration objects. You can read the names and values of the enumeration into arrays, using the System.Enum methods GetNames, GetValues, and GetType. For more information about using these methods, see Information About System.Enum Methods.

For example, to create arrays allNames and allValues for the System.DayOfWeek enumeration, type:

myDay = System.DayOfWeek;
allNames = System.Enum.GetNames(myDay.GetType);
allValues = System.Enum.GetValues(myDay.GetType);

The class of the names array is System.String, while the class of the values array is the enumeration type System.DayOfWeek.

whos all*
  Name       Size  Bytes  Class

  allNames   1x1   112    System.String[]
  allValues  1x1   112    System.DayOfWeek[]

Although the types are different, the information MATLAB displays is the same. For example, type:

allNames(1)
ans = 
Sunday

Type:

allValues(1)
ans = 
Sunday

For an example that uses arrays, see Iterate Through a .NET Enumeration. For information about using System.String, see How MATLAB Handles System.String.

Refer to a .NET Enumeration Member

Overview

You use an enumeration member in your code as an instance of an enumeration. To refer to an enumeration member, use the C# namespace, enumeration, and member names:

Namespace.EnumName.MemberName

For example, the System namespace in the .NET Framework class library has a DayOfWeek enumeration. The members of this enumeration are:

Enumeration members for class 'System.DayOfWeek':
    Sunday
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday

To create a variable with the value Thursday, type:

gameDay = System.DayOfWeek.Thursday;
whos
  Name     Size Bytes Class

  gameDay  1x1  104   System.DayOfWeek

Using the Implicit Constructor

The implicit constructor, Namespace.EnumName, creates a member with the default value of the underlying type. For example, the NetDocEnum.Range enumeration has the following members:

Enumeration members for class 'NetDocEnum.Range':
    Max
    Min

Type:

x = NetDocEnum.Range
whos x
x = 
0

  Name  Size  Bytes  Class

  x     1x1   104    NetDocEnum.Range

Display .NET Enumeration Members as Character Strings

To get the descriptive name of an enumeration, use the char method. For example, type:

gameDay = System.DayOfWeek.Thursday;
['Next volleyball game is ',char(gameDay)]
ans =
Next volleyball game is Thursday

Convert .NET Enumeration Values to Type Double

To convert a value to a MATLAB double, type:

gameDay = System.DayOfWeek.Thursday;
myValue = double(gameDay)
myValue =
     4

Iterate Through a .NET Enumeration

Overview

To display all member names of the System.DayOfWeek enumeration, create a System.String array of names. Use the Length property of this array to find the number of members. For example:

myDay = System.DayOfWeek;
allNames = System.Enum.GetNames(myDay.GetType);
disp(['Members of ' class(myDay)])
for idx = 1:allNames.Length
    disp(allNames(idx))
end
Members of System.DayOfWeek
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Information About System.Enum Methods

To create MATLAB arrays from an enumeration, use the static System.Enum methods GetNames and GetValues. The input argument for these methods is an enumeration type. Use the GetType method for the type of the current instance. To display the signatures for these methods, type:

methodsview('System.Enum')

Look at the following signatures:

QualifiersReturn TypeNameArguments
 System.TypeGetType(System.Enum this)
StaticSystem.String[]GetNames(System.Type enumType)
StaticSystem.ArrayGetValues(System.Type enumType)

To use GetType, create an instance of the enumeration. For example:

myEnum = System.DayOfWeek;

The enumType for myEnum is:

myEnumType = myEnum.GetType;

To create an array of names using the GetNames method, type:

allNames = System.Enum.GetNames(myEnumType);

Alternatively:

allNames = System.Enum.GetNames(myEnum.GetType);

Use .NET Enumerations to Test for Conditions

With relational operators, you can use enumeration members in if and switch statements and other functions that test for equality.

Example Using Switch Statements

The following Reminder function displays a message depending on the day of the week:

function Reminder(day)
% day = System.DayOfWeek enumeration value
% Add error checking here
switch(day)
    case System.DayOfWeek.Monday
        disp('Department meeting at 10:00')
    case System.DayOfWeek.Tuesday
        disp('Meeting Free Day!')
    case {System.DayOfWeek.Wednesday System.DayOfWeek.Friday}
        disp('Team meeting at 2:00')
    case System.DayOfWeek.Thursday
        disp('Volley ball night')
end
end

For example, type:

today = System.DayOfWeek.Wednesday;
Reminder(today)
ans =
Team meeting at 2:00

Example Using Relational Operations

Create the following function to display a message:

function VolleyballMessage(day)
% day = System.DayOfWeek enumeration value
if gt(day,System.DayOfWeek.Thursday)
    disp('See you next week at volleyball.')
else
    disp('See you Thursday!')
end
end

For a day before Thursday:

myDay = System.DayOfWeek.Monday;
VolleyballMessage(myDay)
See you Thursday!

For a day after Thursday:

myDay = System.DayOfWeek.Friday;
VolleyballMessage(myDay)
See you next week at volleyball.

Underlying Enumeration Values

MATLAB supports enumerations of any numeric type.

To find the underlying type of an enumeration, use the System.Enum static method GetUnderlyingType. For example, the following C# statement in the NetDocEnum assembly declares the enumeration Range:

public enum Range : long {Max = 2147483648L,Min = 255L}

To display the underlying type:

maxValue = NetDocEnum.Range.Max;
System.Enum.GetUnderlyingType(maxValue.GetType).FullName
ans = 
System.Int64
Was this topic helpful?