Main Content

setPostSimFcn

SetMATLABfunction to run after each simulation

Description

example

simIn = setPostSimFcn(simIn,func)configures the post simulation function associated with the function handlefuncon theSimulink.SimulationInputobjectsimIn. TheSimulink.SimulationOutputobject is passed as an argument to this function.funcis 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 theSimulink.SimulationOutputoutput to replace the usual logged data or add new data to theSimulink.SimulationOutput对象。

Examples

collapse all

This example specifies a post simulation function for a simulation using aSimulink.SimulationInput对象。

创建一个PostSimFcnthat calculates the mean for logged outputs.

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

创建一个SimulationInputobject for the modelvdp.

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 usingErrorMessageandSimulationMetadataas field names in the function.

Input Arguments

collapse all

Simulation inputs and configuration, specified as aSimulink.SimulationOutput对象。

Function to run after each simulation completes, specified as a function handle or a function name. The software passes theSimulink.SimulationOutput对象。例如:

simIn = simIn.setPostSimFcn(@myPostSim)
wheremyPostSimis a MATLAB function.
functionnewSimOut = myPostSim(simOut);%the function can change the contents of the output object before returning to the clientnewSimOut.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.
functionnewSimOut = myPostSim_additionalArgs(simOut,additionalArg1,additionalArg2)%the function can change the contents of the simulation input after parsim runs the simulationnewSimOut = simOut;end

Output Arguments

collapse all

Simulation configuration with post simulation function added, returned as aSimulink.SimulationInput对象。

Version History

Introduced in R2017a