How to get underlying numbers of enumeration?

153 ビュー (過去 30 日間)
Monika Jaskolka
Monika Jaskolka 2020 年 2 月 25 日
コメント済み: riccardo 2020 年 11 月 9 日
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 Center および File ExchangeData Types についてさらに検索

タグ

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by