Detect Creation of Unused Variables for Unconditional and Conditional Variant Choices
R2026bThis example shows how to detect unused variables in generated code when the variant condition of the source block is unconditional and the variant condition of its destination block is conditional. 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 Variant Sink block has the variant condition expressions V == 1 and V == 2. The Allow zero active variant controls parameter of the block is selected.

Generate C Code Using Embedded Coder
Suppose that the value of V is set to 3.
When you generate code using Embedded Coder®, the variant condition expressions V == 1 and V == 2 evaluate to false. Simulink® disables all the blocks connected to the input and output streams of the Variant Sink block. However, the code generator produces code for all the variant choices in the model.

A variable Add is created in the generated code. During code compilation, this variable remains unused because both choices evaluate to false.

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 unconditional and the variant condition of its destination block is conditional. 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 Add may remain unused because the source block is unconditional while the destination block conditions V == 1 and V == 2 both evaluate 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
Variant condition mismatch at signal source and destination