Guy on Simulink

Simulink & Model-Based Design

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Variants: What Are Your Options in R2018b? 7

Posted by Guy Rouleau,

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.

7 CommentsOldest to Newest

Roberto Paolinelli replied on : 1 of 7
Hello Guy, is there a way to automatically use variant sink/source to tell code generation from simulation? I mean without declaring and changing a simulink parameter. For example: CNTRL B automatically triggers Code Genration Variant passing through for example port #1, CNTRL D rather than PLAY trigger simulation variant passing for example through port #2. I'm trying to achieve it through rtwenvironmentmode(bdroot(gcbh)) but I get an error as if the simulation path was built in code generation. Thank you very much
@Roberto: Unfortunately, this is not possible to make this switch automatically. Trying "rtwenvironmentmode(bdroot(gcbh))" was a good idea, but it happens too early or too late to work properly. Many users have requested the same. We have been working on that for some time, but this is not as simple as it might look. Hopefully, this will be possible in a release or two.
@ Robert: Good point. Dimension Variants are pretty new and I have not personally used them much, so did not think about including this feature in this post. I will look into a follow-up post covering more code generation specific capabilities related to variants.
Marcelo Avila replied on : 5 of 7
Hy, Guy. Thank you for the post, I do not think Variant an easy topic. I have one question. Imagine I have a Library that is a Variant Subsystem with two possible choices. So, I create a new Simulink model and add two instances of this Library in it. I would like to set one instance as the first variant option, and the other instance as the second option. How can I do something like that using Expression Mode? Maybe with a control expression that uses the name of the model as part of its logic? Thank you very much.
@ Marcelo: Unfortunately, it is not possible for two instances of the same variant library block in the same model to have different active variants when controlled by an expression. Only label mode can achieve that. You are not the only one requesting that. We are looking into allowing variant expressions to be evaluated in mask workspaces, which would solve this problem. I cannot say when this will be ready.
Roberto Paolinelli replied on : 7 of 7
Hi Guy, this post is always useful during time... Anyway I'm looking for the possibility of changing model active configuration (reference) according to selected variant. For example a simulink parameter drives a variant object telling HIL, from embedded code generation: at least I will have to change the tlc target (and not only I suppose...). In this case I would like to refer automatically the right configuration: how can I achieve it? Thanks a lot.