Main Content

Iterate Through a .NET Enumeration

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:

NameReturn TypeArgumentsQualifiers
GetTypeSystem.Type(System.Enum this) 
GetNamesSystem.String[](System.Type enumType)Static
GetValuesSystem.Array(System.Type enumType)Static

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

Display Enumeration Member Names

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

Related Topics