Main Content

Conditional Inclusions (#if / #endif)

You can generate preprocessor conditional directives in your code by implementing variants in your model. In the generated code, the preprocessor conditional directives select a section of code to execute at compile time.

This example uses Simulink.Variant objects to control Variant Subsystem blocks. For information about other ways to control variants, see Variant Control Modes in Variant Blocks. For information about types of variants other than Variant Subsystem blocks, see What Are Variants and When to Use Them.

Inspect Model

Open the example model rtwdemo_preprocessor_subsys.

model = 'rtwdemo_preprocessor_subsys';


This model contains two variant subsystems, LeftController and RightController, each with two child subsystems, Linear and Nonlinear. The PostLoadFcn callback function of the model loads rtwdemo_preprocessor_data.mat. To view the callback function, from the Simulink toolstrip, click the Modeling tab and, in the Setup section, click Model Settings > Model Properties. In the Model Properties dialog box, open the Callbacks tab and select PostLoadFcn. Alternatively, view the callback function programmatically.

ans = 
'load rtwdemo_preprocessor_data.mat'

The callback function instantiates two Simulink.Variant objects, LINEAR and NONLINEAR, and one Simulink.Parameter, VSSMODE. The LINEAR variant is active when VSSMODE is equal to 0. The NONLINEAR variant is active when VSSMODE is equal to 1. VSSMODE is initialized to 1. Verify these settings programmatically.

ans = 
ans = 
ans = int32

LeftController and RightController use LINEAR and NONLINEAR as the variant control expressions for their respective Linear and Nonlinear subsystems. To view this configuration, right-click LeftController and click Block Parameters (Subsystem). Alternatively, view the variant control expressions programmatically.

choices = get_param(append(model,'/LeftController'),'VariantChoices');
ans = struct with fields:
         Name: 'LINEAR'
    BlockName: 'rtwdemo_preprocessor_subsys/LeftController/Linear'

ans = struct with fields:
         Name: 'NONLINEAR'
    BlockName: 'rtwdemo_preprocessor_subsys/LeftController/Nonlinear'

Configure Model

Open the Configuration Parameters dialog box. In the left pane, click Code Generation, then confirm System target file is set to ert.tlc. Alternatively, set the parameter programmatically.


In the left pane, click Interface. Confirm the Ignore custom storage classes parameter is cleared. Alternatively, clear the parameter programmatically.


To generate preprocessor conditionals, use custom storage classes.

Open the Block Parameters dialog box for the LeftController subsystem. Verify the Variant activation time parameter is set to code compile. Alternatively, set the parameter programmatically.

set_param(append(model,'/LeftController'),'VariantActivationTime','code compile');

Open the LeftController subsystem. For each child subsystem, click the subsystem and, in the Subsystem Block tab, confirm Is Atomic Subsystem is selected. Alternatively, set the parameters programmatically.


Generate code.

[~,cgDir] = rtwdemodir();
### Starting build procedure for: rtwdemo_preprocessor_subsys
### Successful completion of build procedure for: rtwdemo_preprocessor_subsys

Build Summary

Top model targets built:

Model                        Action                        Rebuild Reason                                    
rtwdemo_preprocessor_subsys  Code generated and compiled.  Code generation information file does not exist.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 17.494s

View the generated code. The generated code includes #if and #endif preprocessor directives that use LINEAR and NONLINEAR to select one variant to compile.

cfile = fullfile(cgDir,'rtwdemo_preprocessor_subsys_ert_rtw','rtwdemo_preprocessor_subsys.c');
rtwdemodbtype(cfile,"  /* Outputs for Atomic SubSystem: '<Root>/LeftController' */", "  /* End of Outputs for SubSystem: '<Root>/LeftController' */", 1, 1);
  /* Outputs for Atomic SubSystem: '<Root>/LeftController' */

  /* Outputs for Atomic SubSystem: '<S1>/Linear' */
  /* VariantMerge generated from: '<S1>/Out1' incorporates:
   *  DiscreteFilter: '<S3>/Discrete Filter'
  rtY.Out1 = rtb_Add - 0.5 * rtDWork.DiscreteFilter_states_a;

  /* Update for DiscreteFilter: '<S3>/Discrete Filter' */
  rtDWork.DiscreteFilter_states_a = rtY.Out1;

  /* End of Outputs for SubSystem: '<S1>/Linear' */

  /* Outputs for Atomic SubSystem: '<S1>/Nonlinear' */
  /* VariantMerge generated from: '<S1>/Out1' incorporates:
   *  DiscreteFilter: '<S4>/Discrete Filter'
   *  Lookup_n-D: '<S4>/Lookup Table'
   *  Sum: '<Root>/Add'
  rtY.Out1 = look1_binlxpw(rtb_Add, rtCP_LookupTable_bp01Data,
    rtCP_LookupTable_tableData, 4U) - 0.5 * rtDWork.DiscreteFilter_states_o;

  /* Update for DiscreteFilter: '<S4>/Discrete Filter' */
  rtDWork.DiscreteFilter_states_o = rtY.Out1;

  /* End of Outputs for SubSystem: '<S1>/Nonlinear' */

Related Topics