Main Content

Detect Creation of Unused Variables for Lenient Variant Choices

R2026b

This example shows how to detect unused variables in generated code when the variant condition of the source block is more lenient than the variant condition of the destination block. To detect these variables, use the Variant condition mismatch at signal source and destination parameter. This parameter helps you debug unused variables in the generated code.

Model Description

In this model, the source of the highlighted signal is the block named EvlnB. The destination of the signal is the block named VariantSource3. The variant condition of EvlnB is A > 0, which is more lenient than the variant condition A > 0 && B == 1 of VariantSource3.

Generate C Code Using Embedded Coder

Suppose that the value of A is 1, and the value of B is 0.

When you generate code using Embedded Coder®, the variant condition of EvlnB, A > 0, evaluates to true, and the condition of VariantSource3, A > 0 && B == 1, evaluates to false. However, code is generated for all variant choices. In the generated code, the variable rtb_Switch is created. This variable corresponds to the Switch block that is located inside the block named EvlnB. The generated code uses rtb_Switch only if A > 0 && B == 1 evaluates to true. In this example, because A > 0 && B == 1 evaluates to false, rtb_Switch remains unused.

Detect Unused Variables by Using a Diagnostic Setting

To identify modeling patterns that might produce unused variables in generated code:

1. In the Simulink Editor, on the Modeling tab, select Model Settings > Diagnostics. Expand the Advanced parameters section and scroll to the bottom of the dialog box.

2. In the Modeling issues related to variants section, set the Variant condition mismatch at signal source and destination parameter to warning. This setting displays a warning when the variant condition of the source block is more lenient than the variant condition of the destination block. The warning helps you identify variables that might remain unused while compiling the code.

3. For this model, generate the code. A warning appears indicating that the variable rtb_Switch might remain unused because the source block condition A > 0 is more lenient than the destination block condition A > 0 && B == 1, which evaluates to false.

Note: The diagnostic flags potential unused variables before code generation. During code generation, the code generator can optimize and remove these variables, so they might not appear in the generated code.

See Also

Topics