MATLAB: How to use .NET enumerated values in MATLAB 7.8 (R2009a)

MATLAB

According to the documentation I can work with enumerated values when using MATLAB's Interface to the .NET Framework. There is no clear code example however, so I am not sure how my enumerated values in C# relate to the enumerated values on the MATLAB side.

Best Answer

Given below is a C# code example which uses enumerators and the MATLAB commands which can be used to work with them.
=============================
C# Code
=============================
using System;
namespace myNamespace
{
public class myClass
{
public enum myEnum {
val1,
val2
}
public myEnum myField;
public String myMethod(myEnum in1) {
return in1.ToString();
}
}
}
=============================
MATLAB Code
=============================
You can load the assembly using:
>> NET.addAssembly('myAssembly.dll')
Then you can create an object using:
>> obj = myNamespace.myClass
Now you can work as follows with this enumerator:
>> obj.myField = myNamespace.myEnum.val1
>> obj.myField = myNamespace.myEnum.val2
>> obj.myMethod(myNamespace.myEnum.val1)
>> obj.myMethod(myNamespace.myEnum.val2)
Concluding, you use: namespace.enum_name.value to specify a certain enumerator value and not namespace.class_name.enum_name.value.
=============================
Further Remarks
=============================
As noted in the documentation there is a limitation to using enumerators with non-Int32 underlying datatypes; you can work around this limitation however using the related solution listed below.