Create Variant Controls Programmatically
Create and Export Variant Controls
Create control variables, define variant conditions, and export control variables.
Create control variables in the global workspace or a data dictionary.
FUEL=2; EMIS=1;
Use the control variables to define the control condition using a
Simulink.Variant
object.LinearController=Simulink.Variant('FUEL==2 && EMIS==1');
Note
Before each simulation, define
Simulink.Variant
objects representing the variant conditions.If you saved the variables in the global workspace, select the control variables to export. Right-click and clickSave Asto specify the name of a MAT-file.
Reuse Variant Conditions
If you want to reuse common variant conditions across models, specify variant control conditions usingSimulink.Variant
objects.
ReuseSimulink.Variant
objects to change the model hierarchy dynamically to reflect variant conditions by changing the values of the control variables that define the condition expression.
The example modelAutoSSVar
shows the use ofSimulink.Variant
objects to define variant control conditions.
Note
You must useSimulink.Variant
objects to define variant control conditions for AUTOSAR workflows.
枚举类型的变量控制
Use enumerated types to give meaningful names to integers used as variant control values.
In the MATLAB®Editor, define the classes that map enumerated values to meaningful names.
classdefsldemo_mrv_CONTROLLER_TYPE < Simulink.IntEnumTypeenumerationNONLINEAR (1) SECOND_ORDER (2)endend
classdef sldemo_mrv_BUILD_TYPE < Simulink.IntEnumType enumeration PROTOTYPE (1) PRODUCTION (2) end end
Define
Simulink.Variant
objects for these classes in the global workspace.VE_NONLINEAR_CONTROLLER = Simulink.Variant... ('E_CTRL==sldemo_mrv_CONTROLLER_TYPE.NONLINEAR') VE_SECOND_ORDER_CONTROLLER =Simulink.Variant... ('E_CTRL==sldemo_mrv_CONTROLLER_TYPE.SECOND_ORDER') VE_PROTOTYPE =Simulink.Variant... ('E_CURRENT_BUILD==sldemo_mrv_BUILD_TYPE.PROTOTYPE') VE_PRODUCTION =Simulink.Variant... ('E_CURRENT_BUILD==sldemo_mrv_BUILD_TYPE.PRODUCTION')
Using enumerated types simplifies the generated code because it contains the names of the values rather than integers.
Related Examples
- Generate Preprocessor Conditionals for Variant Systems(Embedded Coder)
- Create and Validate Variant Configurations