Specify a MATLAB function to run before start of each simulation through SimulationInput object, in


in.PreSimFcn = @(x) myfunction(arg1, arg2...)



in.PreSimFcn = @(x) myfunction(arg1, arg2...) runs before each simulation starts. The Simulink.SimulationInput object is passed as an argument x to this function. myfunction is any MATLAB® function and can be used to modify the Simulink.SimulationInput object. If you use myfunction to modify the Simulink.SimulationInput object, you must return Simulink.SimulationInput object as the only output argument.


collapse all

This example shows how to specify a MATLAB function through SimulationInput object to run at before start of each simulation.

Create a PreSimFcn function.

function presim(in)
signalbuilder('sf_car/User Inputs', 'ActiveGroup', in.Variables.Value)

Open the model.

model = 'sf_car';

Create an array of SimulationInput objects for this model. Use in.PreSimFcn to run presim before simulation.

 n = 4;
   for idx = n:-1:1
      in(idx) = Simulink.SimulationInput(model);
      in(idx) = in(idx).setVariable('SigIndex', idx);
      in(idx).PreSimFcn = @(x) presim(x);

Simulate the model.

out = sim(in)

Input Arguments

collapse all

This is an input to myfunction in which you can modify the Simulink.SimulationInput object.

Introduced in R2017a

Was this topic helpful?