| Contents | Index |
Open model, AutoSSVar.mdl, which contains a variant subsystem.
To recreate the variant control variables specifically for code generation:
Open the Model Explorer and click the Base Workspace.
A variant control variable can be a Simulink.Parameter object or a mpt.Parameter object. In the Model Explorer, remove the current variant control variables, EMIS and FUEL, and re-create them as Simulink.Parameter objects. Select Add and Simulink Parameter to create two variant control variables, EMIS and FUEL.
In the Simulink.Parameter property dialog box, specify the Value as 1 and the Data type as int8 for both EMIS and FUEL.
Specify the Storage class parameter for both FUEL and EMIS as ImportedDefine(Custom):
Specify the value of the variant control variable. Because the storage class is ImportedDefine(Custom), specify the Header File parameter as an external header file, AutoSSVar_variables.h, in the Custom Attributes section of the Simulink.Parameter property dialog box.
Supply the values of the variant control variables in the external header file.
#define FUEL 1 #define EMIS 1
Note The generated code refers to a variant control variable as a user-defined macro. The generated code does not contain the value of the macro. The value of the variant control variable determines the active variant in the compiled code. |
The variant objects for this model already reside in the base workspace. For information on how to create the variant objects, follow the instructions for Creating Variant Objects. Check that only one variant object is active in the generated code by implementing the condition expressions of the variant objects such that only one evaluates to true. The generated code includes a test of the variant objects to determine that there is only one active variant. If this test fails, your code does not compile.
Double-click the Variant Subsystem block, Engine, to display the child subsystems.
For each child subsystem, right-click the subsystem and select Subsystem Parameters from the list. The Block parameters dialog box opens.
To specify each child subsystem as an atomic subsystem, in the Block parameters dialog box, select the Treat as atomic unit parameter.
In order to generate preprocessor conditional directives configure your model as follows:
On the Code Generation pane of the Configuration Parameter dialog box, specify the System target file parameter as ert.tlc and clear Ignore custom storage classes. In order to generate preprocessor conditionals, you must use custom storage classes.
On the Optimization > Signals and ParametersMATLAB pane of the Configuration Parameters dialog box, select Inline parameters.
On the Code Generation > Interface pane of the Configuration Parameter dialog box, select the Enable All option of the Generate preprocessor conditionals parameter. This parameter is a global setting for the parent model and enables generating preprocessor conditionals for all variants of all variant blocks in the model. For more information, see Generate preprocessor conditionals.
On the Code Generation > Report pane of the Configuration Parameter dialog box, select Create code generation report.
The generated code contains all child subsystems of the Variant Subsystem block protected by C preprocessor conditionals. In this case, the selection of the active variant (subsystem) is deferred until the generated code is compiled. Only one variant object, which is encoded in C macros, must evaluate to true.
After building the model, look at the variants in the generated code. AutoSSVar_types.h includes the following:
Call to external header file, AutoSSVar_variables.h, which contains the macro definitions for the variant control variables, FUEL and EMIS.
/* Includes for objects with custom storage classes. */ #include "AutoSSVar_variables.h"
Preprocessor directives defining the variant objects. The values of these macros depend on the value of the variant control variables, FUEL and EMIS. The condition expression associated with each macro determine the active variant.
/* Model Code Variants */ #ifndef DE #define DE ((FUEL == 2) && (EMIS == 2)) #endif #ifndef DU #define DU ((FUEL == 2) && (EMIS == 1)) #endif #ifndef GE #define GE ((FUEL == 1) && (EMIS == 2)) #endif #ifndef GU #define GU ((FUEL == 1) && (EMIS == 1)) #endif
Check that exactly one variant is active at a time:
/* Exactly one variant for '<Root>/Engine' should be active */ #if (GU) + (GE) + (DU) + (DE) != 1 #error Exactly one variant for '<Root>/Engine' should be active #endif
Calls to the step and initialization functions are conditionally compiled as shown in a portion of the step function, AutoSSVar_step, in AutoSSVar.c:
#if DE rtb_MergeForOutportOut1 = 2.2 * AutoSSVar_U.In1; #elif DU rtb_MergeForOutportOut1 = 2.1 * AutoSSVar_U.In1; #elif GE rtb_MergeForOutportOut1 = 1.2 * AutoSSVar_U.In1; #elif GU rtb_MergeForOutportOut1 = 1.1 * AutoSSVar_U.In1; #endif /* DE */
![]() | Model Variants in the Generated Code | Restrictions on Code Generation of a Variant Subsystem | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |