How can I use .NET Enumeration with underlying datatypes other than Int32 in MATLAB 7.8 (R2009a)?

5 views (last 30 days)
My .NET Assembly has a Field which is an enumeration with an underlying datatype of uint:
using System;
namespace TestClass
{
public class TestClass
{
public enum myEnum : uint
{
VAL0 = 0,
VAL1 = 1
}
public myEnum Field1;
}
}
I can successfully load this assembly into MATLAB using NET.addAssembly. But I am unable to set or get the value of Field1. Upon executing the following code:
NET.addAssembly('TestClass.dll')
obj = TestClass.TestClass
obj.Field1
MATLAB generates the following error:
??? Undefined function or method 'display' for input arguments of type 'TestClass.myEnum'.
Additionally, executing the following line:
TestClass.myEnum
causes MATLAB to crash.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 May 2020
Edited: MathWorks Support Team on 5 May 2020
This enhancement has been incorporated in releases R2011a and later.
For these releases, more information on working with .NET enumerations in MATLAB can be found here, and information about the limitations in support of .NET enumerations can be found here.
For previous product releases, read below for any possible workarounds:
MATLAB 7.8 (R2009a) does not support enumeration with underlying datatypes other than Int32. To work around this limitation you can use the attached EnumHelper class which allows you to set enumeration fields and properties using strings.
You can simply download the compiled version of EnumHelper.dll (see below) or if you need to make any changes to the helper you can download the source and compile the DLL yourself.
Once downloaded (and compiled) you should be able to use the class in the following way in MATLAB:
1. Load the assembly
NET.addAssembly('C:\MATLAB\Work\EnumHelper\EnumHelper.dll')
(Replace C:\MATLAB\Work\EnumHelperwith the location were you placed EnumHelper.dll)
2. Instead of calling "obj.Field1", to get the current Field value, you now use:
MATLAB.NET.EnumHelper.GetEnum(obj,'Field1')
Notice that the name of the field is specified as a string.
3. To set the value of the field you can now call:
MATLAB.NET.EnumHelper.SetEnum(obj,'Field1','VAL1')
Again the field name is specified as string, also the enumerator value is given as string. Do not specify the fully qualified name (e.g. "TestClass.myEnum.VAL1") but only the actual value part (e.g. "VAL1").

More Answers (0)

Products


Release

R2009a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!