I am evaluating the reuse of .Net enums from existing .Net assemblies in the Simulink environment. This should prevent that an enumeration is changed in the .Net environment, but not in the Simulink model. In other words: There should not be a need to define enumerations in matlab when they are already defined in .Net and can be reused.
My Simulink model is the following:
I have one "branch" that demonstrates a switch case with double values and below another one that does it with the enumerations which are built into matlab.
classdef (Sealed) mDayOfWeek < Simulink.IntEnumType
enumeration
mSunday(0)
mMonday(1)
mTuesday(2)
mWednesday(3)
mThursday(4)
mFriday(5)
mSaturday(6)
end
end
I have registered to run my go.m (via Model Properties -> Callbacks -> PreLoadFcn) when I open my Simulink model. It prepopulates the variables Eingabe and mEingabe with default values as follows:
if exist('Eingabe', 'var') == 0
Eingabe = 0;
end
if exist('mEingabe', 'var') == 0
mEingabe = mDayOfWeek.mSunday;
end
Now I want to add a third branch and that should use a the enum System.DayOfWeek from mscorlib.dll. The initialization is not the problem:
if exist('sEingabe', 'var') == 0
sEingabe = System.DayOfWeek.Sunday;
end
However, I do not even succeed in configuring a constant block with a constant value from System.DayOfWeek:
Is there something I am missing? Is it possible at all? Do I need to write some conversion to accomplish it?
MathWorks support told me that using .NET object instances as signals in Simulink is not supported. I was encouraged to use the "int32" function to cast the .NET enum value to the corresponding integer value by setting the value of the constant block to
int32(System.DayOfWeek.Thursday)
and to configure the SwitchCase block as follows:
{int32(System.DayOfWeek.Thursday), int32(System.DayOfWeek.Friday)}
However I was told that this will not work in combination with any code generation capabilities of Matlab/Simulink. If that is required (like in my case) one could automatically let Matlab generate an appropriate Matlab enumeration with the help of .NET reflection to keep the enums in sync. That would look something like this (not tested):
function genEnum(classname)
% Use .NET refection to get Type information
t = System.Type.GetType('System.DayOfWeek');
% Create a new M-file with the name of the Enum
f = fopen([char(t.Name) '.m'],'w');
% Write the classdef header with the name of the class
fprintf(f,'classdef %s < Simulink.IntEnumType\n',char(t.Name));
% Open enumeration
fprintf(f,'\tenumeration\n');
% Get all Enum Values
v = t.GetEnumValues;
for i=1:v.Length
% Add each value
fprintf(f,'\t\t%s(%d)\n',char(v(i)),int32(v(i)));
end
% Close the class and enum definitions
fprintf(f,'\tend\nend\n');
% Close the file
fclose(f);
Finally I decided instead in favor of a T4 template of this kind, which appears to me much straightforward:
classdef (Sealed) mDayOfWeek < Simulink.IntEnumType
enumeration
<#
var arr = System.Enum.GetValues(typeof(System.DayOfWeek));
foreach(var it in arr)
{
string name = it.ToString();
string value = ((int)it).ToString();
#>
<#= name #>(<#= value #>)
<#
}
#>
end
end