Control Algorithm Execution Using Enumerated Signal
This example shows how to use a signal of an enumerated data type to control the execution of a block algorithm. For basic information about using enumerated data types in models, see Use Enumerated Data in Simulink Models.
Define Enumerated Type
Copy the enumerated type definition ex_SwitchCase_MyColors
into a script file in your current folder.
classdef ex_SwitchCase_MyColors < Simulink.IntEnumType enumeration Red(0) Yellow(1) Blue(2) Mauve(3) end end
Alternatively, you can use the function Simulink.defineIntEnumType
to define the type.
Simulink.defineIntEnumType('ex_SwitchCase_MyColors',... {'Red','Yellow','Blue','Mauve'},[0;1;2;3])
Explore Example Model
Open the example model ex_enum_switch_case
.
open_system('ex_enum_switch_case')
Open the Enumerated Constant block dialog box. The constant output value is ex_SwitchCase_MyColors.Blue
.
Open the Switch Case block dialog box. The Case conditions box is specified as a cell array containing three of the four possible enumeration members. The block has four outputs corresponding to the three specified enumeration members and a default case.
Open the Switch Case Action Subsystem blocks. The subsystems each contain a Constant block that uses a different constant value.
Control Execution During Simulation
Simulate the model. The Display block shows the value 5
, which corresponds to the case ex_SwitchCase_MyColors.Blue
.
In the Enumerated Constant block dialog box, specify Value as ex_SwitchCase_MyColors.Red
and click Apply. The Display block shows 19
.
Specify Value as ex_SwitchCase_MyColors.Mauve
and click Apply. The Display block shows 3
, which corresponds to the default case.