MATLAB: Do I get redundant if conditions when I use a multirate system with SingleTasking mode

Embedded Coder

When I generate code from the attached model I receive the following code
54 if (GroupCode_M->Timing.TaskCounters.TID[1] == 0) {
55 /* Gain: '<Root>/TsB' incorporates:
56 * Inport: '<Root>/in1'
57 */
58 GroupCode_B.TsB = GroupCode_P.TsB_Gain * GroupCode_U.in1;
59 }
60
61 if (GroupCode_M->Timing.TaskCounters.TID[1] == 0) {
62 /* Gain: '<Root>/TsB1' incorporates:
63 * Inport: '<Root>/in1'
64 */
65 GroupCode_B.TsB1 = GroupCode_P.TsB1_Gain * GroupCode_U.in1;
66 }
67
68 if (GroupCode_M->Timing.TaskCounters.TID[1] == 0) {
69 /* Gain: '<Root>/TsB2' incorporates:
70 * Inport: '<Root>/in1'
71 */
72 GroupCode_B.TsB2 = GroupCode_P.TsB2_Gain * GroupCode_U.in1;
73 }
Is there a way or an optimization to just have one if statement in this code?

Best Answer

If you would like to have just one if statement in the generated code you can group the blocks with the same sample rates in one subsystem and set the subsystem to an atomic unit and set the option
"Real-Time Workshop system code" set to "Inline" then you will receive the following code:
54 if (GroupCode_v2_M->Timing.TaskCounters.TID[1] == 0) {
55 /* Outputs for atomic SubSystem: '<Root>/Subsystem' */
56
57 /* Gain: '<S1>/TsB' incorporates:
58 * Inport: '<Root>/in1'
59 */
60 GroupCode_v2_B.TsB = GroupCode_v2_P.TsB_Gain * GroupCode_v2_U.in1;
61
62 /* Gain: '<S1>/TsB1' incorporates:
63 * Inport: '<Root>/in1'
64 */
65 GroupCode_v2_B.TsB1 = GroupCode_v2_P.TsB1_Gain * GroupCode_v2_U.in1;
66
67 /* Gain: '<S1>/TsB2' incorporates:
68 * Inport: '<Root>/in1'
69 */
70 GroupCode_v2_B.TsB2 = GroupCode_v2_P.TsB2_Gain * GroupCode_v2_U.in1;
71
72 /* end of Outputs for SubSystem: '<Root>/Subsystem' */
73 }