Main Content

simulink.compiler.modifyParameters

Tune block parameters at runtime via workspace variables

Description

simulink.compiler.modifyParameters(modelName) tunes block parameters 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.

Examples

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, mRefUsingGlobalWksVars. 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.

open_system("example_modify_parameters.slx");

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';
load_system(model);
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);
end

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),...
                     Simulink.Simulation.Variable('gDen',0.5)];
    simulink.compiler.modifyParameters(model,newGlobalVars);
end
if time==2.5
    % Modify variables in reference model workspace
    newRefWksVars = [Simulink.Simulation.Variable('gNum',1.2),...
                     Simulink.Simulation.Variable('gDen',0.1)];
    simulink.compiler.modifyParameters(model,newRefWksVars);
end
if time==4.5
    % Modify variables used by mask dialog parameters
    newMaskVars = [Simulink.Simulation.Variable('mGain',2.0),...
                   Simulink.Simulation.Variable('Bias',-1,'Workspace',model)];
    simulink.compiler.modifyParameters(model,newMaskVars);
end
end

Input Arguments

collapse all

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

Introduced in R2021b