Run Simulations for Variant Models Using Variant Configurations
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 slexVariantConfigsSimulation 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 = "slexVariantConfigsSimulation";
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");
[03-Mar-2025 11:07:10] Running simulations... [03-Mar-2025 11:07:16] 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");
[03-Mar-2025 11:07:17] Checking for availability of parallel pool... [03-Mar-2025 11:07:17] Starting Simulink on parallel workers... [03-Mar-2025 11:07:19] Configuring simulation cache folder on parallel workers... [03-Mar-2025 11:07:19] Loading model on parallel workers... [03-Mar-2025 11:07:22] Running simulations... [03-Mar-2025 11:07:28] Completed 1 of 2 simulation runs [03-Mar-2025 11:07:28] Received simulation output (size: 7.90 KB) for run 1 from parallel worker. [03-Mar-2025 11:07:40] Completed 2 of 2 simulation runs [03-Mar-2025 11:07:40] Received simulation output (size: 7.90 KB) for run 2 from parallel worker. [03-Mar-2025 11:07:40] Cleaning up parallel workers...