Main Content


Tune block parameters at runtime via workspace variables

Since R2021b


simulink.compiler.modifyParameters(modelName,Vars) tunes block parameters specified by an array of Simulink.Simulation.Variables objects Vars at simulation runtime via workspace variables. You can use simulink.compiler.modifyParameters to modify variables only during a running simulation.

You can use simulink.compiler.modifyParameters to tune any variables that are returned by the simulink.compiler.getTunableVariables function. simulink.compiler.modifyParameters is supported only for rapid accelerator and deployment workflows.

You can use simulink.compiler.modifyParameters only at simulation runtime with one of the Simulink® Compiler™ callback functions, simulink.compiler.setPostStepFcn, simulink.compiler.setExternalInputsFcn or simulink.compiler.setExternalOutputsFcn.


collapse all

This example shows you how to use the simulink.compiler.modifyParameters function to tune block parameters.

Open the Model

The example model example_modify_parameters referneces another model, exRefUsingGlobalWksVars. The top model gain block, the triggered subsystem and the gain block in the referenced model all use global workspace variables. The masked subsystem present in the model uses a global variable and a model workspace variable.


Write a Function for Runtime Parameter Tuning

The following function sets the simulation mode to rapid and creates a Simulink.SimulationInput object. In this function, you can use simulink.compiler.setPostStepFcn API to set a callback which uses simulink.compiler.modifyParameters to tune block parameters

function runtimeParameterTuning()
simMode = 'rapid';
model = 'example_modify_parameters';
ref = 'exRefModelWorkspaceVars';
closeModels = onCleanup(@() cellfun(@(x)close_system(x,0),{model,ref}));
set_param(model, "SimulationMode", simMode);

% Get simulation input object
simInput = Simulink.SimulationInput(model);
% Set post-step callback function that tunes variables
simInput = simulink.compiler.setPostStepFcn(simInput,@(time)postStepParameterTuner(time,model));

out = sim(simInput);

Write a Function for the Post-Step Callback

The function postStepParameterTuner uses simulink.compiler.modifyParameters to modify the variables.

function postStepParameterTuner(time,model)
% Callback which tunes parameters based on time
if time==5.0
    % Modify global variables used by top model gain block
    newGlobalVars = [Simulink.Simulation.Variable('gNum',1.1),...
if time==2.5
    % Modify variables in reference model workspace
    newRefWksVars = [Simulink.Simulation.Variable('gNum',1.2),...
if time==4.5
    % Modify variables used by mask dialog parameters
    newMaskVars = [Simulink.Simulation.Variable('mGain',2.0),...

Input Arguments

collapse all

Name of model for which you want to find tunable parameters, specified as a string.

Array of simulink.compiler.variable objects that specifies the names and new values of the parameters to be modified, specified as a string.

Version History

Introduced in R2021b