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.
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='ConditionalInput';
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');
Build the model
slbuild(model)
### Starting build procedure for: ConditionalInput ### Successful completion of build procedure for: ConditionalInput Build Summary Top model targets: Model Build Reason Status Build Duration =================================================================================================================== ConditionalInput Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 11.813s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 12.624s
View the generated code without the optimization. These lines of code are in the ConditionalInput.c
file.
cfile = fullfile('ConditionalInput_grt_rtw','ConditionalInput.c'); coder.example.extractLines(cfile,'/* Model step', '/* Model initialize', 1, 0);
/* Model step function */ void ConditionalInput_step(void) { real_T rtb_Switch1; /* Switch: '<Root>/ Switch2' incorporates: * Constant: '<Root>/C_5' * Gain: '<Root>/ G3' * Inport: '<Root>/input' * RelationalOperator: '<Root>/Relational Operator' * Sum: '<Root>/ Sum' */ if (ConditionalInput_U.input >= -5.0) { rtb_Switch1 = 3.0 * ConditionalInput_U.input; } else { rtb_Switch1 = ConditionalInput_U.input - 10.0; } /* End of Switch: '<Root>/ Switch2' */ /* Switch: '<Root>/Switch1' incorporates: * Constant: '<Root>/C5' * Inport: '<Root>/input' * RelationalOperator: '<Root>/Relational Operator1' */ if (ConditionalInput_U.input >= 5.0) { /* Outport: '<Root>/output' incorporates: * Constant: '<Root>/ C10' * Sum: '<Root>/ Sum1' */ ConditionalInput_Y.output = ConditionalInput_U.input + 10.0; } else { /* Outport: '<Root>/output' */ ConditionalInput_Y.output = rtb_Switch1; } /* End of Switch: '<Root>/Switch1' */ }
The generated code contains an if-else
statement for the Switch2
block and an if
statement for the Switch1
block. Therefore, the generated code for Switch1:Path2
executes even if the if
statement for Switch1:Path1
evaluates to true.
Enable Optimization
Open the Configuration Parameters dialog box.
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
slbuild(model) cfile = fullfile('ConditionalInput_grt_rtw','ConditionalInput.c'); coder.example.extractLines(cfile,'/* Model step', '/* Model initialize', 1, 0);
### Starting build procedure for: ConditionalInput ### Successful completion of build procedure for: ConditionalInput Build Summary Top model targets: Model Build Reason Status Build Duration =============================================================================================== ConditionalInput Generated code was out of date. Code generated and compiled. 0h 0m 10.606s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 11.918s /* Model step function */ void ConditionalInput_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 (ConditionalInput_U.input >= 5.0) { /* Outport: '<Root>/output' incorporates: * Constant: '<Root>/ C10' * Sum: '<Root>/ Sum1' */ ConditionalInput_Y.output = ConditionalInput_U.input + 10.0; } else if (ConditionalInput_U.input >= -5.0) { /* Switch: '<Root>/ Switch2' incorporates: * Gain: '<Root>/ G3' * Outport: '<Root>/output' */ ConditionalInput_Y.output = 3.0 * ConditionalInput_U.input; } else { /* Outport: '<Root>/output' incorporates: * Sum: '<Root>/ Sum' * Switch: '<Root>/ Switch2' */ ConditionalInput_Y.output = ConditionalInput_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)
See Also
Conditional input branch execution | Switch | Multiport Switch