How to get underlying numbers of enumeration?

153 次查看(过去 30 天)
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?

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Types 的更多信息

标签

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by