Use .NET Enumerations in MATLAB
R2026bIn MATLAB®, an enumeration is a class that defines a fixed set of named values.
Similarly, a .NET enumeration provides a fixed set of members, underlying values, and
methods. You can work with .NET enumerations in much the same way as MATLAB enumerations. Some operations, such as creating arrays of enumeration
values, require using methods of the .NET System.Enum class.
View and Create .NET Enumeration Members
An enumeration member is an instance of an enumeration
class. You can assign .NET enumeration members to MATLAB variables and create MATLAB arrays of members. To display the member names of a .NET enumeration,
use the MATLAB
enumeration function.
For example, display the members of the .NET enumeration
System.DayOfWeek.
enumeration("System.DayOfWeek")
Enumeration members for class 'System.DayOfWeek':
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
SaturdayThis syntax creates a System.DayOfWeek object with the default
enumeration value.
enumObj = System.DayOfWeek
enumObj = Sunday
Create a System.DayOfWeek object with the value
Thursday.
day = System.DayOfWeek.Thursday
day = Thursday
Create Arrays of .NET Enumeration Members
To create MATLAB arrays from a .NET enumeration, use the static
System.Enum methods GetNames and
GetValues. These methods require the enumeration type,
which you can obtain using the GetType method.
For example, create a string array of all the days of the week.
allDays = string(System.Enum.GetNames(enumObj.GetType))
allDays =
1×7 string array
"Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"To create an array of System.DayOfWeek enumeration members,
use System.Enum.GetValues.
allValues = System.Enum.GetValues(enumObj.GetType)
allValues =
DayOfWeek[] with properties:
Length: 7
LongLength: 7
Rank: 1
SyncRoot: [1×1 System.DayOfWeek[]]
IsReadOnly: 0
IsFixedSize: 1
IsSynchronized: 0Work with .NET Enumerations in MATLAB
To work with .NET enumeration members in MATLAB, you can use the methods of the .NET enumeration or other .NET methods. You can also use built-in MATLAB functions and operators, including relational operators, conversion functions, and bitwise functions.
.NET Methods
To view the available methods and supported MATLAB operations for a .NET
enumeration class, use the methods function.
methods("System.DayOfWeek")Methods for class System.DayOfWeek: CompareTo GetTypeCode double intersect keyMatch setxor strncmpi DayOfWeek HasFlag eq isequal le strcmp union Equals ToString ge isequaln lt strcmpi GetHashCode cellstr gt ismember ne string GetType char int32 keyHash setdiff strncmp Static methods: Format GetNames GetValues Parse ToObject GetName GetUnderlyingType IsDefined ReferenceEquals
You can call a method of a .NET enumeration class by using dot notation with a
.NET member object. For example, check if two
System.DayOfWeek enumeration members are equal.
day.Equals(enumObj)
ans =
logical
0You can also pass a .NET enumeration object to a .NET method from a different
class. For example, pass a System.DayOfWeek value to the
GetAbbreviatedDayName method of the
System.Globalization.DateTimeFormatInfo class.
dtformat = System.Globalization.DateTimeFormatInfo; dtformat.GetAbbreviatedDayName(day)
ans =
ThuRelational Operators with Enumerations
You can use MATLAB relational operators with .NET enumeration members. These
relational operators are helpful if you want to use .NET enumeration members in
control flow statements, such as if and
switch.
For example, use an if statement to display a message
depending on the day of the week.
if gt(day,System.DayOfWeek.Thursday) disp("Have a good weekend!") else disp("Have a good week!") end
Enumeration Conversion Functions
You can use MATLAB conversion functions (including string,
double, and other numeric functions) on .NET
enumeration members.
For example, convert an enumeration member to a string so that you can include it in message text.
gameDay = System.DayOfWeek.Thursday;
"Next game is " + string(gameDay)
ans =
"Next game is Thursday"If you convert an enumeration member using a numeric conversion function, you can access its underlying value.
underlyingVal = double(gameDay)
ans =
4Bitwise Functions
Some .NET enumerations define bit flags using the
System.Flags attribute. Bitwise operations allow you to
combine or test multiple flag values. MATLAB provides these functions for bitwise operations: bitand, bitnot, bitor, and bitxor (available only for
enumerations with the System.Flags attribute)
To use bitwise enumeration functions, the .NET enumeration must:
Include the
Flagsattribute.Define values that are powers of
2.
For more information, see Use Bit Flags with .NET Enumerations.
Underlying Enumeration Values
MATLAB supports .NET enumerations of any numeric underlying type. Use the
static System.Enum.GetUnderlyingType method to identify the
numeric type.
For example, find the underlying type of System.DayOfWeek. The
underlying type is System.Int32.
thisDay = System.DateTime.Today; dow = thisDay.DayOfWeek; System.Enum.GetUnderlyingType(dow.GetType).FullName
ans = System.Int32
You can use the underlying numeric value for calculations. For example, compute
the first day of the current week. The conversion function
double returns the underlying value of
dow as part of the calculation.
startDateOfWeek = AddDays(thisDay,-double(dow)); ToShortDateString(startDateOfWeek)
ans = 2/8/2026
See Also
bitand | bitnot | bitor | bitxor