setPostSimFcn - Set MATLAB function to run after each simulation - MATLAB (original) (raw)
Main Content
Set MATLAB function to run after each simulation
Syntax
Description
simIn = setPostSimFcn([simIn](#mw%5F2801a25c-998a-4f2c-86a1-6f36b62bb14d),[func](#mw%5F1588e927-5ebd-4cad-bea8-655a6fc8bf6a))
configures the post-simulation function associated with the function handlefunc
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.
Examples
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 newSimOut = postsim(simOut); newSimOut = simOut newSimOut.mean = mean(simOut.yout(:,1)); 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.
View the result from the post simulation function.
As a best practice, avoid using ErrorMessage
andSimulationMetadata
as field names in the function.
Input Arguments
Simulation inputs and configuration, specified as aSimulink.SimulationOutput
object.
Function to run after each simulation completes, specified as a function handle or a function name. The software processes theSimulink.SimulationOutput
object. For example:
simIn = simIn.setPostSimFcn(@myPostSim)
wheremyPostSim
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 takeSimulink.SimulatinInput
object as a second argument. You can have a PostSimFcn
with this syntax wherex
is the SimulationOutput
object and y
is the SimulationInput
object:
simIn = setPostSimFcn(simIn, @(x,y) myPostSimFcn(x,y));
Output Arguments
Version History
Introduced in R2017a