Main Content

setPreSimFcn

Specify MATLAB function to run before start of each simulation on Simulink.SimulationInput object

Description

simIn = setPreSimFcn(simIn,func) registers the callback function associated with the function handle func before each simulation starts. The Simulink.SimulationInput object is passed as an argument to this function. func is any MATLAB® function and can be used to modify the Simulink.SimulationInput object. If you use func to modify the Simulink.SimulationInput object, you must return the Simulink.SimulationInput object as the only output argument.

Input Arguments

collapse all

Simulation inputs and configuration, specified as a Simulink.SimulationInput object.

Function to run before each simulation, specified as a function handle or a function name. setPreSimFcn passes the Simulink.SimulationInput object in the shape of x argument. The value of func has to be a Simulink.SimulationInput object to be used in a simulation. For example:

simIn = setPreSimFcn(simIn,@myPreSim)
In this example, myPreSim is a MATLAB function.
% The function can change the contents of the simulation input before parsim runs the simulation.
function newSimIn = myPreSim(simIn); 
    % Start by preserving the information already in the simulation input.  
    newSimIn = simIn; 
    % Add a new model parameter to your simulation input in the preSimFcn.
    newSimIn = newSimIn.setModelParameter("myParameter","StopTime","10"); 
end

You can also specify the pre-simulation function as a function handle with additional inputs.

% The function can change the contents of the simulation input before parsim runs the simulation.
function newSimIn = myPreSim_additionalArgs(simIn,additionalArg1,additionalArg2) 
    % Start by preserving the information already in the simulation input.
    newSimIn = simIn;
    % Add a new model parameter to simIn as an additional argument.
    newSimIn = newSimIn.setModelParameter("StopTime",additionalArg1); 
end

Output Arguments

collapse all

Simulation configuration with presimulation function added, returned as a Simulink.SimulationInput object.

Version History

Introduced in R2017a