Main Content

setVariantConfiguration

Set variant configuration for simulation using SimulationInput object

Since R2024a

Description

simin = setVariantConfiguration(simin,varconfigname) sets the variant configuration varconfigname for the simulation configured using the Simulink.SimulationInput object simin.

For models that use variant elements in Simulink®, such as variant blocks and variant parameters, you can create named variant configurations using Variant Manager for Simulink. A variant configuration represents a combination of variant choices across the model hierarchy. Using Variant Manager, you define a variant configuration as a set of variant control variables and their values, which you can use to activate a specific variant path in the hierarchy. Use the setVariantConfiguration method to specify the variant configuration to apply to the model before the simulation runs.

example

Examples

collapse all

This example shows how to programmatically run single or multiple simulations for a variant model by specifying variant configurations as input to the Simulink.SimulationInput object.

Variant elements in Simulink®, including variant blocks and variant parameters, enable you to represent multiple design alternatives within a single model. For such a model, you can create variant configurations to represent combinations of variant choices across the model hierarchy. These configurations comprise a set of variant control variables and their corresponding values, allowing you to activate specific variants within the model hierarchy.

Note: You must install the Variant Manager for Simulink support package using Add-On Explorer to create variant configurations for a model.

Explore Example Model

The slexVariantManagement model has multiple named variant configurations created using Variant Manager. The configurations are stored in a variant configuration data object, vcd, defined in the data dictionary associated with the model.

Open the model:

modelName = "slexVariantManagement";
open_system(modelName);

View the variant configurations in vcd:

vcd = Simulink.VariantManager.getConfigurationData(modelName);
cellfun(@(name)(fprintf("%s\n", name)),{vcd.Configurations(:).Name});
LinInterExpNoNoise
LinInterExpWithNoise
LinInterStd
NonLinExterLowFid
NonLinExterHighFid
SmartAIExterHighFid
LinExterHighFid

Run Single Simulation

To simulate the model using a named configuration in vcd, create a Simulink.SimulationInput object, set the VariantConfiguration property in the object to the named configuration, and run the simulation. This variant configuration is temporarily applied to the model, activating only the corresponding variant path in the model hierarchy before simulation.

simInp = Simulink.SimulationInput(modelName);
simInp = setVariantConfiguration(simInp,'LinInterExpWithNoise');
simOut = sim(simInp,"ShowProgress","on");
[22-Apr-2024 16:05:15] Running simulations...
[22-Apr-2024 16:05:32] Completed 1 of 1 simulation runs

Run Multiple Parallel Simulations

To run multiple simulations using several named configurations present in vcd, create an array of Simulink.SimulationInput objects and set the required configurations. You can use the parsim or batchsim functions to run the simulations in parallel or in batch mode.

simInputs(1:2) = Simulink.SimulationInput(modelName);
simInputs = setVariantConfiguration...
    (simInputs,{'LinInterExpWithNoise','NonLinExterLowFid'});
simOuts = parsim(simInputs,"ShowProgress","on");
[22-Apr-2024 16:05:37] Checking for availability of parallel pool...
Starting parallel pool (parpool) using the 'Processes' profile ...
22-Apr-2024 16:06:47: Job Queued. Waiting for parallel pool job with ID 1 to start ...
Connected to parallel pool with 8 workers.
[22-Apr-2024 16:07:35] Starting Simulink on parallel workers...
[22-Apr-2024 16:08:21] Configuring simulation cache folder on parallel workers...
[22-Apr-2024 16:08:22] Loading model on parallel workers...
[22-Apr-2024 16:08:40] Running simulations...
[22-Apr-2024 16:08:50] Completed 1 of 2 simulation runs
[22-Apr-2024 16:08:50] Completed 2 of 2 simulation runs
[22-Apr-2024 16:08:50] Cleaning up parallel workers...

Input Arguments

collapse all

Simulation specification in which to set the variant configuration name, specified as a Simulink.SimulationInput object or an array of Simulink.SimulationInput objects.

Example: simin = setVariantConfiguration(simin,'NonLinExterLowFid') sets the variant configuration in the simulation configuration stored in the SimulationInput object simin.

Name of variant configuration to apply to the model before simulation runs, specified as a string, a character vector, or a cell array of character vectors.

Example: simin = setVariantConfiguration(simin,'NonLinExterLowFid') sets the variant configuration in the simulation configuration stored in the SimulationInput object simin.

Data Types: char | string

Output Arguments

collapse all

Simulation specification with specified variant configuration name, returned as a Simulink.SimulationInput object or an array of Simulink.SimulationInput objects.

Version History

Introduced in R2024a