In Simulink®, you can use the variant blocks to create a single model that caters to multiple variant requirements. Such models have a fixed common structure and a finite set of variable components. The variable components are activated depending on the variant choice that you select. Thus, the resultant active model is a combination of the fixed structure and the variable components based on the variant choice.
The use of variant blocks in a model helps in reusability of the model for different conditional expressions called variant choices. This approach helps you to meet diverse customer requirements based on application, cost, or operational considerations.
You can use these variants blocks depending on the model design:
Variant Subsystem: For hierarchical model structure.
Inline Variants: For flat model structure.
Note: You can convert a Subsystem block available in a model to a Variant Subsystem block. To do so, right-click the Subsystem block, in the context menu, click Subsystem & Model Reference > Convert Subsystem to > Variant Subsystem.
Suppose you want to simulate a model that represents an automobile with several configurations. These configurations, although similar in several aspects, can differ in properties such as fuel consumption, engine size, or emission standard. Instead of designing multiple models that together represent all possible configurations, you can use variants to model only the varying configurations. This approach keeps the common components fixed.
This model contains two variant choices inside a single Variant Subsystem block. Variant choices are two or more configurations of a component in your model.
Using variants in Model-Based Design provides several advantages:
Variants provide you a way to design one model for many systems.
You can rapidly prototype design possibilities as variants without having to comment out sections of your model.
Variants help you develop modular design platforms that facilitate reuse and customization. This approach improves workflow speed by reducing complexity.
If a model component has several alternative configurations, you can efficiently explore these varying alternatives without altering the fixed, unvarying components.
You can use different variant configurations for simulation or code generation from the same model.
You can simulate every design possibility in a combinatorial fashion for a given test suite.
If you are working with large-scale designs, you can distribute the process of testing these designs on a cluster of multicore computers. Alternatively, you can map different test suites to design alternatives for efficiently managing design-specific tests.
You can generate a reduced model with a subset of configuration from a master model with many variants.
Variants help you specify multiple implementations of a model in a single, unified block diagram. Here are three scenarios where you can use variants:
Models that represent multiple simulation, code generation, or testing workflows.
Models that contain multiple design choices at the component level.
Subsystem blocks representing variant choices can have inports and outports that differ in number from the inports and outports in the parent Variant Subsystem block. See Mapping Inports and Outports of Variant Choices.
Models that are mostly similar but have slight variations, such as in cases where you want to separate a test model from a debugging model.
The test model on the left has a fixed design. On the right, the same test model includes a variant that is introduced for debugging purposes.
Simulink selects the active variant during update diagram time and during code compile time.
You can represent one or more variants as variant choices inside these blocks.
Variant Source and Variant Sink blocks
Variant Subsystem block
Model Variant block
Variant choice representation
Number of ports
Subsystem or Model block
Only Model block
Allows choice hierarchy
Mismatched number of inports and outports among variant choices
Simulink disables inactive ports
Simulink disables inactive ports
Requires manual resolution of disconnected signal lines
Option to specify default variant
Supports control ports
Can be saved as standalone file
Supports physical modeling connection ports
In addition, you can represent variant choices using Variant Source and Variant Sink block. These blocks enable the propagation of variant conditions and they allow you to visualize variant choices in a single layer of your model.
You can create variants at several levels inside your model hierarchy.
A Variant Subsystem is a container of variants choices that are represented as Subsystem or Model blocks. The inputs that the Variant Subsystem block receives from upstream models components map to the inports and outports of the variant choices.
Subsystem and Model blocks representing variant choices can have inports and outports that differ in number from the inports and outports in the parent Variant Subsystem block. However, the following conditions must be satisfied:
The names of the inports of a variant choice are a subset of the inport names used by the parent variant subsystem.
The names of the outports of a variant choice are a subset of the outport names used by the parent variant subsystem.
During simulation, Simulink disables the inactive ports in a Variant Subsystem block. If you are specifying variant choices in a Model Variant block, resolve any disconnected signal lines manually when you switch between choices.
Each Variant block has a badge associated with it. The color and icon of a Variant badge indicate the status of the Variant block. It also provides quick access to few Variant commands. You can right-click the Variant badge to access these commands.
Default Variant badge when no option is selected.
Variant block with Override variant conditions and use the following variant option selected.
Variant block with Allow zero active variant controls option selected.
Variant block with Analyze all choices during update diagram and generate preprocessor conditionals option selected.
Variant block with Propagate conditions outside of variant subsystem option selected.
Variant block with Analyze all choices during update diagram and generate preprocessor conditionals and Allow zero active variant controls option selected.
Variant block with Analyze all choices during update diagram and generate preprocessor conditionals and Propagate conditions outside of variant subsystem option selected.
Consider when you want to simulate a Simulink model by excluding some of its blocks from simulation and without physically removing the blocks from the model. The Comment Out and Comment Through commands in Simulink provide you with an option to exclude blocks from simulation. Depending on your modeling requirement, you can use these options:
Comment Out: Excludes the selected block from simulation. The signals are terminated and grounded.
Comment Through: Excludes the selected block from simulation. The signals are passed through. To comment through a block, the number of input ports and the output ports for the block must be same.
To access the Comment Out or the Comment Through options, right-click the block and in the context menu either select Comment Out or Comment Through based on your modeling requirement.
Alternatively, you can also select the block and press Cltr+Shift+X to comment out or press Cltr+Shift+Y to comment through.
You can use
to view or change the commented state of a block programmatically.
get_param(gcb,'commented'); % To view the
commented state of the block
set_param(gcb,'commented','on'); % To comment
out a block
set_param(gcb,'commented','through'); % To
comment through a block
set_param(gcb,'commented','off'); % To uncomment
When you comment out a block, the signal names at the output port of the block are ignored. To include such signals during simulation, the signal name must be added at the input port of the block.
Comment Out and Comment Through are not supported with these blocks: Inport, Outport, Duplicate Port, Connection ports, Argument Inport, Argument Outport, Data Store Memory, Signal Generator, Goto Tag Visibility, For, and While blocks.