How to get underlying numbers of enumeration?

158 Ansichten (letzte 30 Tage)
Monika Jaskolka
Monika Jaskolka am 25 Feb. 2020
Kommentiert: riccardo am 9 Nov. 2020
I have a custom enumeration data type such as the one defined in the code below. How can I get the underlying integer values that comprise the type (i.e., [0,1,2])?
Simulink.defineIntEnumType('BasicColors', {'Red', 'Yellow', 'Blue'}, [0;1;2])
The getEnumTypeInfo function does not provide this information. The class metadata provides the EnumerationMemberList, but not the numerical values. Is there any other way?

Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 26 Feb. 2020
Simulink.defineIntEnumType('BasicColors', {'Red', 'Yellow', 'Blue'}, [0;1;2]);
a=BasicColors.Blue;
a.real
  6 Kommentare
Jonas
Jonas am 13 Okt. 2020
Like magic. Really clever! Never would I found this out.
Thanks a lot!
Jonas
riccardo
riccardo am 9 Nov. 2020
I'm using 2020a.
Once you have an EnumTypeDefinition object in the main workspace, one of the properties is "Enumerals".
>> enumDefList = <enumTypeDefObject>.Enumerals;
will return in "enumDefList" a structure array holding Name, Value and Description of the enumerated type.
Alternatively, if you define the object in Matlab:
>>Simulink.defineIntEnumType( 'tryEnum01', { 'AA', 'BB', 'CC', 'KK' }, [0, 2 , 4, 6] )
using enumeration you extract the arrays of enumerated definitions and labels :
>> [ gg, hh ] = enumeration( 'tryEnum01' );
scalar-expanding the enumerated definitions "gg" within a cast to double:
>> indexes = double( gg(:) )
indexes =
0
2
4
6
<<
If you want just the numerical values, then
>> indexes = double( enumeration( 'tryEnum01' ) )
indexes =
0
2
4
6

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by