simulink.compiler.setExternalOutputsFcn
Set callback to read external root outport block data after each simulation step
Since R2020b
Syntax
Description
in = simulink.compiler.setExternalOutputsFcn(
function registers a callback to dynamically process the values for every output port at the
root level of a model during simulation. in
, @(id, time, data) processOutput(id, time, data)
)
Examples
Deploy App with Live Simulation Results of Lorenz System
This example shows how to develop an app that uses callbacks for simulation inputs and outputs to view the simulation of a Simulink® model of the Lorenz system. You can then deploy the app with Simulink Compiler™.
Open the Lorenz System App
This example uses a Simulink model of the Lorenz System, and an app that is created in the MATLAB® App Designer that simulates the model with different input and output values. To learn more about how to create an app using the App Designer, see Create and Run a Simple App Using App Designer. To open the app, type the following at the MATLAB command window.
LorenzSystemApp
App Details
Open the LorenzSystemApp
.mlapp
file. You can view the code written to create this app in the Code View section of App Designer. The essential part of building this app is the behavior of the Simulate button. It has the following salient parts: creating the SimulationInput
object, configuring it for deployment, using simulation callbacks to read the output port data and plot the data at each time step. These three functions allow you to see the live results of the simulation in the deployed app.
Create the Simulink.SimulationInput
Object
In the function createSimulationInput
, define an empty Simulink.SimulationInput
object for the model. Use this Simulink.SimulationInput
object to set simulation callbacks and variables for the model.
Use the simulation callback functions register the callback operations. The simulink.compiler.setPostStepFcn
function registers a callback that is invoked after every simulation step. The simulink.compiler.setExternalOuputsFcn
registers a callback that dynamically processes the values for every output port at the root level of a model during simulation.
Use the setVariable
function to provide the parameter values to the app through the Simulink.SimulationInput
object. Values for the simulation are obtained from the edit fields of the UI of the app. To deploy the app, use the simulink.compiler.configureForDeployment
function. (Comment the line of code that calls simulink.compiler.configureForDeployment
function for faster debugging.)
function simInp = createSimulationInput(app) % Create an empty SimulationInput object simInp = Simulink.SimulationInput('LorenzSystemModel'); simInp = simulink.compiler.setSimulationStatusChangeFcn(simInp, ... @(simStatus) app.simStatusChangedFcn(simStatus)); % PostStepFcn is used to update plots simInp = simulink.compiler.setPostStepFcn(simInp, ... @(simTime) app.postStepFcn(simTime), ... 'Decimation',app.postStepFcnDecimation); % Load the parameters values from the ui edit fields simInp = simInp.setVariable('rho',app.rhoUIC.Value); simInp = simInp.setVariable('beta',app.betaUIC.Value); simInp = simInp.setVariable('sigma',app.sigmaUIC.Value); simInp = simInp.setVariable('x0',app.x0UIC.Value); simInp = simInp.setVariable('y0',app.y0UIC.Value); simInp = simInp.setVariable('z0',app.z0UIC.Value); % Configure simInp for deployment simInp = simulink.compiler.configureForDeployment(simInp); end % createSimulationInput
Simulation Callback Functions
The simulation callback functions register callbacks that allow you to read values from the output ports and to write values to the root input ports. These functions register callbacks at every simulation time step, which allows you to view live results of the simulation.
The postStepFcn
Callback
The postStepFcn
callback function is invoked after every simulation step. The time
argument is the time for the previous simulation step. The postStepFcn
function obtains the cached outport block values for every simTime
and uses those values to the plot the cached values at simulation time.
function postStepFcn(app, simTime) app.SimTime.Text = sprintf('%0.5g',simTime); % 0.1234e+56 wcTime = toc(app.wallClockTimeAtSimStart); app.SimPace.Text = sprintf('%7.2f', simTime/wcTime); % 1234.56 app.adjustPostStepFcnDecimation(); simOut = simulink.compiler.getSimulationOutput(app.modelName); ts = extractTimetable(simOut.yout); xv = []; yv = []; zv = []; for its = 1:length(ts) idx = find(ts{its}.Time >= app.simTimeAtLastPostStep); switch ts{its}.Name case 'x' xv = ts{its}.Data(idx); case 'y' yv = ts{its}.Data(idx); case 'z' zv = ts{its}.Data(idx); end end addpoints(app.hLine,xv,yv,zv); [xv, yv, zv] = getpoints(app.hLine); set(app.hTail,'XData',xv(1),'YData',yv(1),'ZData',zv(1)); set(app.hHead,'XData',xv(end),'YData',yv(end),'ZData',zv(end)); drawnow limitrate; app.simTimeAtLastPostStep = simTime; end
Test in App Designer
Before deploying the application, ensure that the app runs in the App Designer. Click Simulate to verify that the application works by simulating the model for different values.
Compile App for Deployment
You can use the App Designer to compile and deploy the app. You can also use the deploytool
function. For more information on compiling and deploying with the App Designer, see Develop Apps Using App Designer, Web Apps and Application Compiler.
To compile the app in this example, use the mcc
command followed by the app name.
mcc -m LorenzSystemApp
Input Arguments
in
— Simulation inputs
Simulink.SimulationInput
object
Simulation inputs and changes to model for simulation, specified as a
Simulink.SimulationInput
object
Example: in = Simulink.SimulationInput('vdp')
@(id, time, data) processOutput(id, time, data)
— Function handle for callback
MATLAB® function handle
Function handle for callback to process outputs with the values,
data
for every root port block specified by id
at simulation step time, time
.
id
– A root outport block index, for which the callback is set, specified by a numerical value.time
– Time for which the input to the root outport block is required, specified by a numeric value.data
– Value for the root outport block.
Version History
Introduced in R2020b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)