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.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. 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.SimulationOutput object.

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

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

Output Arguments

collapse all

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

Version History

Introduced in R2017a