Main Content

setPostSimFcn

Set MATLAB function to run after each simulation

Description

simIn = setPostSimFcn(simIn,func) configures the post-simulation function associated with the function handle func on the Simulink.SimulationInput object simIn. The Simulink.SimulationOutput 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. Post-processed data, must be returned 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.

example

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.

openExample('simulink_general/VanDerPolOscillatorExample');
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.SimulationOutput object.

Function to run after each simulation completes, specified as a function handle or a function name. The software processes the Simulink.SimulationOutput object. For example:

simIn = simIn.setPostSimFcn(@myPostSim)
where myPostSim is a MATLAB function.
% The function can change the contents of the output object before returning to the client.
function newSimOut = myPostSim(simOut)
% Post process the output for only the relevant values. 
newSimOut = simOut;
newSimOut.meanValue = (simOut.yout(:,1)); 
end
You can also specify the post-simulation function as a function handle with additional arguments.
% The function can change the contents of the simulation input after parsim runs the simulation.
function newSimOut = myPostSim_additionalArgs(simOut,additionalArg1,additionalArg2) 
     newSimOut = simOut;
end

The PostSimFcn can also take Simulink.SimulatinInput object as a second argument. You can have a PostSimFcn with this syntax where x is the SimulationOutput object and y is the SimulationInput object:

simIn = setPostSimFcn(simIn, @(x,y) myPostSimFcn(x,y));

Output Arguments

collapse all

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

Version History

Introduced in R2017a