MATLAB Examples

Use Conditional Input Branch Execution

This example shows how to optimize the generated code for a model that contains Switch and Multiport Switch blocks. When you select the model configuration parameter Conditional input branch execution, Simulink executes only blocks that compute the control input and data input that the control input selects. This optimization improves execution speed.

Contents

Example Model

In this example, switch paths are conditionally executed. If Switch1 control input is true, Switch1 executes blocks grouped in the Switch1:Path1 branch. If Switch1 control input is false, Switch1 executes blocks grouped in the Switch1:Path2 branch. If Switch1 executes blocks in the Switch1:Path2 branch and Switch2 control input is true, Switch2 executes blocks in the Switch2:Path1 branch. If Switch2 control input is false, Switch2 executes blocks in the Switch2:Path2 branch. The pseudo code shows this logic.

model='rtwdemo_condinput';
open_system(model);

Generate Code

The Conditional input branch execution parameter is on by default. Enter the following command-line API to turn off the parameter.

set_param(model, 'ConditionallyExecuteInputs', 'off');

Create a temporary folder for the build and inspection process.

currentDir=pwd;
[~,cgDir]=rtwdemodir();

Build the model.

rtwbuild(model)
### Starting build procedure for model: rtwdemo_condinput
### Successful completion of build procedure for model: rtwdemo_condinput

View the generated code without the optimization. These lines of code are in the rtwdemo_condinput.c file.

cfile = fullfile(cgDir,'rtwdemo_condinput_grt_rtw','rtwdemo_condinput.c');
rtwdemodbtype(cfile,'/* Model step', '/* Model initialize', 1, 0);
/* Model step function */
void rtwdemo_condinput_step(void)
{
  /* Switch: '<Root>/ Switch2' incorporates:
   *  Constant: '<Root>/ C_10'
   *  Constant: '<Root>/C_5'
   *  Gain: '<Root>/  G3'
   *  Inport: '<Root>/input'
   *  RelationalOperator: '<Root>/Relational Operator'
   *  Sum: '<Root>/ Sum'
   */
  if (rtwdemo_condinput_U.input >= -5.0) {
    rtwdemo_condinput_Y.output = 3.0 * rtwdemo_condinput_U.input;
  } else {
    rtwdemo_condinput_Y.output = rtwdemo_condinput_U.input + -10.0;
  }

  /* End of Switch: '<Root>/ Switch2' */

  /* Switch: '<Root>/Switch1' incorporates:
   *  Constant: '<Root>/C5'
   *  Inport: '<Root>/input'
   *  RelationalOperator: '<Root>/Relational Operator1'
   */
  if (rtwdemo_condinput_U.input >= 5.0) {
    /* Outport: '<Root>/output' incorporates:
     *  Constant: '<Root>/  C10'
     *  Sum: '<Root>/ Sum1'
     */
    rtwdemo_condinput_Y.output = rtwdemo_condinput_U.input + 10.0;
  }

  /* End of Switch: '<Root>/Switch1' */
}

The generated code contains an if-else statement for the Switch1 block and an if statement for the Switch2 block. Therefore, the generated code for Switch1:Path2 executes even if the if statement for Switch1:Path1 evaluates to true.

Enable Optimization

  1. Open the Configuration Parameters dialog box.
  2. Select the Conditional input branch execution parameter. Alternatively, you can use the command-line API to enable the optimization.
set_param(model, 'ConditionallyExecuteInputs','on');

Generate Code with Optimization

rtwbuild(model)
cfile = fullfile(cgDir,'rtwdemo_condinput_grt_rtw','rtwdemo_condinput.c');
rtwdemodbtype(cfile,'/* Model step', '/* Model initialize', 1, 0);
### Starting build procedure for model: rtwdemo_condinput
### Successful completion of build procedure for model: rtwdemo_condinput

/* Model step function */
void rtwdemo_condinput_step(void)
{
  /* Switch: '<Root>/Switch1' incorporates:
   *  Constant: '<Root>/C5'
   *  Constant: '<Root>/C_5'
   *  Inport: '<Root>/input'
   *  RelationalOperator: '<Root>/Relational Operator'
   *  RelationalOperator: '<Root>/Relational Operator1'
   *  Switch: '<Root>/ Switch2'
   */
  if (rtwdemo_condinput_U.input >= 5.0) {
    /* Outport: '<Root>/output' incorporates:
     *  Constant: '<Root>/  C10'
     *  Sum: '<Root>/ Sum1'
     */
    rtwdemo_condinput_Y.output = rtwdemo_condinput_U.input + 10.0;
  } else if (rtwdemo_condinput_U.input >= -5.0) {
    /* Outport: '<Root>/output' incorporates:
     *  Gain: '<Root>/  G3'
     *  Switch: '<Root>/ Switch2'
     */
    rtwdemo_condinput_Y.output = 3.0 * rtwdemo_condinput_U.input;
  } else {
    /* Outport: '<Root>/output' incorporates:
     *  Constant: '<Root>/ C_10'
     *  Sum: '<Root>/ Sum'
     *  Switch: '<Root>/ Switch2'
     */
    rtwdemo_condinput_Y.output = rtwdemo_condinput_U.input + -10.0;
  }

  /* End of Switch: '<Root>/Switch1' */
}

The generated code contains one if statement. The generated code for Switch1:Path2 only executes if the if statement evaluates to false.

Close Model and Code Generation Report

bdclose(model)
rtwdemoclean;
cd(currentDir)