How to get underlying numbers of enumeration?

142 visualizaciones (últimos 30 días)
Monika Jaskolka
Monika Jaskolka el 25 de Feb. de 2020
Comentada: riccardo el 9 de Nov. de 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?

Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 26 de Feb. de 2020
Simulink.defineIntEnumType('BasicColors', {'Red', 'Yellow', 'Blue'}, [0;1;2]);
a=BasicColors.Blue;
a.real
  6 comentarios
Jonas
Jonas el 13 de Oct. de 2020
Like magic. Really clever! Never would I found this out.
Thanks a lot!
Jonas
riccardo
riccardo el 9 de Nov. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by