Main Content


Specify a MATLAB function to run after each simulation is complete through SimulationInput object, in



in = in.setPostSimFcn(func) registers the callback function associated with the function handle func after each simulation is complete. The Simulink.SimulationInput object is passed as an argument to this function. func is any MATLAB® function and can be used to do the post processing on the output. To return post processed data, you must return it as values in a struct. These values are then packed into the Simulink.SimulationOutput output to replace the usual logged data or add new data to the Simulink.SimulationOutput object.


collapse all

This example specifies a MATLAB Function through SimulationInput object to run after each simulation is complete.

Create a PostSimFcn to get the mean of the output.

function newout = postsim(out);
newout.mean = mean(out.yout);

Create a SimulationInput object for a model.

in = Simulink.SimulationInput('vdp');
in = in.setPostSimFcn(@(x) postsim(x));
in = in.setModelParameter('SaveOutput','on');

Simulate the model.

out = sim(in)

View your result


It is best practice to avoid using 'ErrorMessage' and 'SimulationMetadata' as field names in the function.

Input Arguments

collapse all

Function to run after each simulation completes, specified as a function handle or a function name. setPostSimFcn passes the Simulink.SimulationInput object in the shape of y argument. For example:

in = in.setPreSimFcn(@myfunc, y)
where myfunc is a MATLAB function such as
function f = myfunc(x)
f ...%Compute function value with x as an input argument
You can also specify func as a function handle for an anonymous function:
in = in.setPostSimFcn(@(y) myfunction(arg1, arg2 ...))

Version History

Introduced in R2017a