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.
function newSimIn = myPreSim(simIn); %the function can change the contents of the simulation input before parsim runs the simulation
    newSimIn = simIn; %start by preserving the information already in the simulation input
    newSimIn = newSimIn.setModelParameter("myParameter","StopTime","10"); %now we could, for example, add a new model parameter to our simulation input in the preSimFcn
end

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

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

Output Arguments

collapse all

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

Version History

Introduced in R2017a