convert an enum to another type of enum

조회 수: 53 (최근 30일)
James Marriott
James Marriott 2018년 9월 12일
답변: Hiral L 2019년 12월 11일
I have an enum of for example 'Gender' (Male =0 , Female =1) and I have another enum own 'MyGender' enum (Male =0 , Female =1, Unknown =2)
My question is how can I write something quick and nice to convert from their enum to mine?
In Matlab it is possible to do this:
Var1 = Gender.Male
Var2 = MyGender.(char(Var1))
But when I do this is a state machine is simulink I get the following error:
Simulink does not have enough information to determine output sizes for this block. If you think the errors below are
inaccurate, try specifying types for the block inputs and/or sizes for the block outputs.
Component:Stateflow | Category:Coder error
Non-constant expression or empty matrix. This expression must be constant because its value determines the size or
class of some expression.
Can someone suggest a better implementation that will work with code gen also.
Thanks

채택된 답변

TAB
TAB 2018년 9월 17일
If your enums definitions are compatible, then you can convert them directly in Simulink using "Data Type Conversion" block.
Another option, convert the first enum value to integer and then convert integer to second enum type.

추가 답변 (1개)

Hiral L
Hiral L 2019년 12월 11일
Hi all I know this topic is a bit old but I wanted to include a code example.
//Valid
Color c = (Color)3;
//Result: c = Blue
//Invalid
Color c = (Color)4;
//Result: c = 4
Code taken from this blog: c# convert int to enum

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by