| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Real-Time Workshop Embedded Coder |
| Contents | Index |
| Learn more about Real-Time Workshop Embedded Coder |
| On this page… |
|---|
How to Generate Code Variations for Model Variants Reviewing Code Variants in the Code Generation Report |
The Real-Time Workshop Embedded Coder software generates code variants from a Simulink® model containing model reference variants. To learn how to create a model containing model reference variants, see Using Model Reference Variants in the Simulink documentation. The Real-Time Workshop Embedded Coder software generates code for all variants of a referenced model. In the generated code, variants are preprocessor conditional directives that determine the active variant and the sections of code to execute.
When you implement variants in the generated code, you can:
Reuse generated code from a set of application models that share functionality with minor variations.
Share generated code with a third party that activates one of the variants in the code.
Validate all of the supported variants for a model and then choose to activate one variant for a particular application, without regenerating and revalidating the code.
To learn about variant control variables and variant objects see Implementing Model Reference Variants in the Simulink documentation. Variant control variables used for code generation have different requirements than variant control variables used for simulation. Perform the following steps to define variant control variables for generating code.
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, select Add and choose either Simulink Parameter or MPT Parameter. Specify a name for the new parameter.
On the Simulink.Parameter or mpt.Parameter property dialog box, specify the Storage class parameter by choosing one of the following:
ImportedDefine(Custom) custom storage class.
CompilerFlag(Custom) custom storage class.
A user-defined storage class created using the Custom Storage Class Designer. Your storage class must have the Data initialization parameter set to Macro and the Data scope parameter set to Imported. See Using the Custom Storage Class Designer for more information.
Note If the storage class is CompilerFlag(Custom) then the makefile options (for example, OPTOPTS) must specify the macro and its value. |
If the storage class is either ImportedDefine(Custom) or a user-defined custom storage class, specify the Header File parameter as an external header file 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. 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.
Follow the instructions for Implementing Variant Objects for model reference variants to implement variant objects for code generation. Ensure 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 might not compile.
In order to generate preprocessor conditional directives configure your model as follows:
On the Optimization pane of the Configuration Parameters dialog box, select Inline parameters.
On the Real-Time Workshop pane of the Configuration Parameter dialog box, clear Ignore custom storage classes. Otherwise, the code generator ignores all custom storage class specifications, and treats all data objects as if their storage class were Auto.
On the Interface pane of the Configuration Parameter dialog box, select the Use Local Settings option of the Generate preprocessor conditionals parameter. This parameter is a global setting for the parent model. This setting enables the Generate preprocessor conditionals parameter located in the Model block parameters dialog box. See Generate preprocessor conditionals for more information.
Open the Model block parameters dialog box for a variant model block. Select the Generate preprocessor conditionals parameter.
In the Model block parameters dialog box, clear the Override variant conditions and use following variant parameter.
Build your model.
The Code Variants Report displays a list of the variant objects, their condition, and the model blocks that use them. The Code Variants Report also includes a section that lists the model blocks that have variants. In the contents section of the code generation report, click the link to the Code Variants Report:

To open a model for generating preprocessor conditionals, enter rtwdemo_preprocessor.
After building the model, look at the variants in the generated code. rtwdemo_preprocessor_types.h includes the following:
Call to external header file, rtwdemo_preprocessor_macros.h, which contains the macro definition for the variant control variable, MODE.
/* Includes for objects with custom storage classes. */ #include "rtwdemo_importedmacros.h"
Preprocessor directives defining the variant objects, LINEAR and NONLINEAR. The values of these macros depend on the value of the variant control variable, MODE. The condition expression associated with each macro, LINEAR and NONLINEAR, determine the active variant.
/* Model Code Variants */ #ifndef LINEAR #define LINEAR (MODE == 0) #endif #ifndef NONLINEAR #define NONLINEAR (MODE == 1) #endif
A check to ensure that exactly one variant is active at a time:
/* Exactly one variant for '<Root>/Left Controller' should be active */ #if (LINEAR) + (NONLINEAR) != 1 #error Exactly one variant for '<Root>/Left Controller' should be active #endif
Calls to the step and initialization functions are conditionally compiled as shown in a portion of the step function, rtwdemo_preprocessor_step, in ModRefVar.c:
/* ModelReference: '<Root>/Left Controller' */
#if LINEAR
mr_rtwdemo_linl(&rtb_Add, &rtb_LeftController_merge_1,
&(rtwdemo_preprocessor_DWork.LeftController_1_DWORK1.rtdw));
#endif /* LINEAR */
/* ModelReference: '<Root>/Left Controller' */
#if NONLINEAR
mr_rtwdemo_nlinl(&rtb_Add, &rtb_LeftController_merge_1,
&(rtwdemo_preprocessor_DWork.LeftController_2_DWORK1.rtdw));
#endif /* NONLINEAR */
and
/* ModelReference: '<Root>/Right Controller' */
#if LINEAR
mr_rtwdemo_linr(&rtb_Add1, &rtb_RightController_merge_1,
&(rtwdemo_preprocessor_DWork.RightController_1_DWORK1.rtdw));
#endif /* LINEAR */
/* ModelReference: '<Root>/Right Controller' */
#if NONLINEAR
mr_rtwdemo_nlinr(&rtb_Add1, &rtb_RightController_merge_1,
&(rtwdemo_preprocessor_DWork.RightController_2_DWORK1.rtdw));
#endif /* NONLINEAR */
To construct model reference variants step by step and generate preprocessor directives in the generated code, see the demo rtwdemo_preprocessor_script.
![]() | Architecture Considerations | Creating and Using Host-Based Shared Libraries | ![]() |

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