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.
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 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.
Now, let's see the options available to control which variant is active.
Controlling Variants using Label Mode
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:
Once the block is configured, you can change the active variant by right-clicking on the variant badge displayed on the block:
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.
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.
Manual Variant Sink and Source
Usage of these blocks is pretty straightforward: Double-click on the block to toggle which branch is active.
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.
要发表评论，请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。