Pass System.Enum Arguments

Call .NET Methods with System.Enum Arguments

An example of an enumeration is System.DayOfWeek. To see how to call a .NET method with this input type, use the GetAbbreviatedDayName method in the System.Globalization.DateTimeFormatInfo class. The following code displays the abbreviation for "Thursday".

% Create a DayOfWeek object
thisDay = System.DayOfWeek.Thursday;
formatObj = System.Globalization.DateTimeFormatInfo;
% Display the abbreviated name of the specified day based on the 
% culture associated with the current DateTimeFormatInfo object.
formatObj.GetAbbreviatedDayName(thisDay)

To view the function signature for the GetAbbreviatedDayName method, type:

methodsview('System.Globalization.DateTimeFormatInfo')

Search the list for GetAbbreviatedDayName.

Return TypeNameArguments
System.String RetValGetAbbreviatedDayName( System.Globalization.
DateTimeFormatInfo this,
System.DayOfWeek dayofweek)

For more information, see:

Use System.Enum in MATLAB

In MATLAB®, an enumeration is a class having a finite set of named instances. You can work with .NET enumerations using features of the MATLAB enumeration class and some features unique to the .NET Framework. Some ways to use the System.DayOfWeek enumeration in MATLAB:

  • Display an enumeration member. For example:

    myDay = System.DateTime.Today;
    disp(myDay.DayOfWeek)
  • Use an enumeration in comparison statements. For example:

    myDay = System.DateTime.Today;
    switch(myDay.DayOfWeek)
        case {System.DayOfWeek.Saturday,System.DayOfWeek.Sunday}
            disp('Weekend')
        otherwise
            disp('Work day')
    end
  • Perform calculations using Using the NetDocEnum Example Assembly. For example, the underlying type of DayOfWeek is System.Int32 which you can use to perform integer arithmetic. To display the date of the first day of the current week, type:

    myDay = System.DateTime.Today;
    dow = myDay.DayOfWeek;
    startDateOfWeek = AddDays(myDay,-double(dow));
    ToShortDateString(startDateOfWeek)
    
  • Perform bit-wise operations. For examples, see Creating .NET Enumeration Bit Flags.

For more information, see:

Was this topic helpful?