MATLAB Answers

How to get underlying numbers of enumeration?

76 views (last 30 days)
Monika Jaskolka
Monika Jaskolka on 25 Feb 2020
Commented: Jonas on 13 Oct 2020 at 14:55
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?

  0 Comments

Sign in to comment.

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 26 Feb 2020
Simulink.defineIntEnumType('BasicColors', {'Red', 'Yellow', 'Blue'}, [0;1;2]);
a=BasicColors.Blue;
a.real

  5 Comments

Show 2 older comments
Jonas
Jonas on 13 Oct 2020 at 8:04
This does not seem to work for me. I have created an Simulink.Enumeration called 'ALM_A' in a Data Dictionary, and then copied it to the Base Workspace.
Now i want to get the enumeration value for the entries in this object in MATLAB.
I have tried
ALM_A.STOP
but it returns:
Unrecognized method, property, or field 'STOP' for class 'Simulink.data.dictionary.EnumTypeDefinition'.
When I request 'ALM_A', it returns the enumeration entries, but not the values:
ALM_A =
Simulink.data.dictionary.EnumTypeDefinition
NULL
RESET
NOTIFY
STOP
I have tried finding the possible commands to use on an enumeration type but could not figure it out. I have requested methods(ALM_A) and found some functions, but not yet a way to get the enumeration value from an entry.
I have attached the Simulink.Enumeration object to this message.
Many thanks,
Jonas
Fangjun Jiang
Fangjun Jiang on 13 Oct 2020 at 12:39
When you define a class, you need to run a .m file or run Simulink.defineIntEnumType() to have it established first in the memory. Your "ALM_A" is a Simulink.data.dictionary.EnumTypeDefinition, it needs to be used in the context of a data dictionary.
Here is what I did.
Load the .mat file
run "daexplr" in Command Window, File, New, Data Dictionary
Drag "ALM_A" from base worksapce to the data dictionary
Click menu Add, MATLAB Variable, Specify the value of "Var" as "ALM_A.NULL"
Drag "Var" from data dictionary to base workspace
type "Var" or "ALM_A.STOP" in Command Window
Jonas
Jonas on 13 Oct 2020 at 14:55
Like magic. Really clever! Never would I found this out.
Thanks a lot!
Jonas

Sign in to comment.

More Answers (0)

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by