Guy on Simulink

Simulink & Model-Based Design

Variants: What Are Your Options in R2018b?

I posted about variants in the past, but things have changed a lot since then, so I thought it would be a good idea to review the options available for variants in R2018b.

Variant Subsystem

If you are not familiar with the concept of variants, I recommend getting started with the documentation page What Are Variants and When to Use Them. In short, the concept of variants allows you to have blocks and subsystems that are present in the model, but conditionally compiled.

For the Variant Subsystem, it can contain a set of variant implementations and only one of them can be active at a time. Here is an image giving an overview of the Variant Subsystem.

Variant Subsystem

Variant Sink and Source

Variant Source and Sink blocks are blocks with either multiple inputs and one output (Source) or one input and multiple outputs (Sink). Depending on different conditions that I will discuss later, only one port is active and blocks connected to the other ports are not included during model compilation.

In the following example, the first input is active. The Sine Wave, the "sin" and the square root are inactive and grayed out because they are not needed.

variant source

Now, let's see the options available to control which variant is active.

Controlling Variants using Label Mode

Previously named "Override variant condition", the Label mode is mostly targeting users who want to specify the active variants interactively.

When you open the block parameters dialog of a variant (Subsystem, Source, or Sink -- they all work the same), you can configure the variant using these steps:

Configuring Label Mode

Once the block is configured, you can change the active variant by right-clicking on the variant badge displayed on the block:

Configuring Label Mode

If you prefer, you can also use set_param to change the active variant programmatically.

Controlling Variants using Expression Mode

With Expression Mode, you specify an expression for each variant. This expression must be a valid MATLAB expression that evaluates to true or false.

Configuring Expression Mode

Code Generation

When variants are controlled by an expression, you have additional capabilities related to code generation. The most important one is to generate preprocessor conditional.

In the next image, I specified "a==1" and "a==2" as conditions in both the Variant Source and Sink blocks and I defined "a" as a Simulink.Parameter object in the base workspace. When generating code, both conditions are present and protected by preprocessor conditionals. This allows you to generate code only once and then build the different configurations.

generate preprocessor conditional

Manual Variant Sink and Source

One last item to highlight: the Manual Variant Source and Sink blocks.

Usage of these blocks is pretty straightforward: Double-click on the block to toggle which branch is active.

Manual Variant

During model compilation, only blocks connected to the active branch will be compiled.

Now it's your turn

Let us know what you think of these options and how you control variants in your models.

  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.