Main Content

Prepare Variant-Containing Model for Code Generation

Using Embedded Coder®, you can generate code from Simulink® models containing one or more variant choices. The generated code contains preprocessor conditionals that control the activation of each variant choice.


Simulink supports using multi-instance referenced models with variant Simulink Functions for code generation.

For information on using STF_make_rtw_hook file to customize build process, see Customize Build Process with STF_make_rtw_hook File (Simulink Coder).

Convert Variant Control Variables into Simulink.Parameter Objects

MATLAB® variables allow you to rapidly prototype variant control expressions when you are building your model and generate preprocessor conditionals for code generation. However, if you want to specify other code generation attributes (such as data type), you can convert MATLAB variables into Simulink.Parameter objects.

  1. Specify the model in which you want to replace MATLAB variant control variables with Simulink.Parameter objects.

    model = 'my_model_containing_variant_choices';
  2. Get the variables that are referenced in variant control expressions.

    vars = Simulink.VariantManager.findVariantControlVars(model)
    vars = 
    4x1 struct array with fields:
  3. Create an external header file for specifying variant control values so that the variable definitions are imported when the code runs.

    headerFileName = [model '_importedDefines.h'];
    headerPreamble = strrep(upper(headerFileName),'.','_');
    fid = fopen(headerFileName,'w+');
    fidErr = (fid == -1);
    if (fidErr)
        fprintf('There was an error creating header file %s:\n',...
        fprintf('+++ Creating header file ''%s'' with variant control variable definitions.\n\n',...
        fprintf(fid, '#ifndef %s\n', headerPreamble);
        fprintf(fid, '#define %s\n', headerPreamble);

    Variant control variables defined as Simulink.Parameter objects can have one of these storage classes.

    • Define or ImportedDefine with header file specified

    • CompilerFlag

    • SystemConstant (AUTOSAR)

    • Your own storage class that defines data as a macro


    If you generate code with startup activation time, specify the supported custom storage class for the objects. For more information on built-in and custom storage classes supported with startup activation time see Storage Classes for Different Variant Activation Times.

  4. Loop through all the MATLAB variables to convert them into Simulink.Parameter objects.

    count = 0;
    for countVars = 1:length(vars)
        var = vars(countVars).Name;
        val = vars(countVars).Value;
        if isa(val, 'Simulink.Parameter')
            % Do nothing 
        count = count+1;
    % Create and configure Simulink.Parameter objects 
    % corresponding to the control variable names.
    % Specify the storage class as Define (Custom).
    	newVal = Simulink.Parameter(val);
    	newVal.DataType = 'int16';
    	newVal.CoderInfo.StorageClass = 'Custom';
    	newVal.CoderInfo.CustomStorageClass = 'Define (Custom)';
    	newVal.CoderInfo.CustomAttributes.HeaderFile = headerFileName;
   , var, newVal);
    		if ~fidErr
        		fprintf(fid, '#endif\n');


The header file can be empty for the Define storage class.

Configure Model for Generating Preprocessor Conditionals

If you represent variant choices inside a Variant Subsystem block or a Variant Model block, code generated for each variant choice is enclosed within C preprocessor conditionals #if, #else, #elif, and #endif.

If you represent variant choices using a Variant Source block or a Variant Sink block, code generated for each variant choice is enclosed within C preprocessor conditionals #if and #endif.

Therefore, the active variant is selected at compile time and the preprocessor conditionals determine which sections of the code to execute.


You must have an Embedded Coder license to generate code.

  1. In the Modeling tab of the Simulink toolstrip, click Model Settings.

  2. Select the Code Generation pane, and set System target file to ert.tlc.

  3. In the Report pane, select Create code generation report.


    In the Code Placement pane, if Compact option is selected from File packaging format drop-down list, model_types.h file is not generated and contents of model_types.h file are moved to model.h file.

  4. Select the Code Generation pane, and clear Ignore custom storage classes and Apply.

  5. In your model, right-click the block containing the variant choices (Variant Subsystem, Variant Source, Variant Sink, or Variant Model block) and select Block Parameters.

  6. Ensure that Expression (default option) is selected for Variant control mode parameter.

  7. From the Variant activation time list, select code compile.

    Simulink analyzes all variant choices during an update diagram or simulation. This analysis provides early validation of the code generation readiness for all variant choices.

  8. Build the model.

Related Examples

More About