Effacer les filtres
Effacer les filtres

How to get underlying numbers of enumeration?

153 vues (au cours des 30 derniers jours)
Monika Jaskolka
Monika Jaskolka le 25 Fév 2020
Commenté : riccardo le 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?

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by