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


in.PostSimFcn = @(y) myfunction(arg1, arg2...)



in.PostSimFcn = @(y) myfunction(arg1, arg2...) runs after each simulation is complete. The Simulink.SimulationOutput object is passed as the argument y to this function. myfunction 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.PostSimFcn = @(x) postsim(x);
in = setModelParameter(in, '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

This is a Simulink.SimulationOutput object which is an input to myfunction.

Introduced in R2017a

Was this topic helpful?