Main Content

setPostSimFcn

Set MATLAB function to run after each simulation

Description

example

simIn = setPostSimFcn(simIn,func) configures the post simulation function associated with the function handle func on the Simulink.SimulationInput object simIn. 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 structure. 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.

Examples

collapse all

This example specifies a post simulation function for a simulation using a Simulink.SimulationInput object.

Create a PostSimFcn that calculates the mean for logged outputs.

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

Create a SimulationInput object for the model vdp.

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

Simulate the model.

out = sim(simIn);

View the result from the post simulation function.

out.mean

As a best practice, avoid using ErrorMessage and SimulationMetadata as field names in the function.

Input Arguments

collapse all

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

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

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

Output Arguments

collapse all

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

Version History

Introduced in R2017a