主要内容

金宝appsimulink.compiler.setpoststepfcn

Register a callback to run after each simulation step

描述

in = 金宝appsimulink.compiler.setpoststepfcn(,@(time) postStepFcn(time))函数注册一个在每个模拟步骤后调用的回调。

例子

全部收缩

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 and Examine the Project File

This example uses a Simulink project that contains all the files required to run this example. The project contains a Simulink® model of the Lorenz system and an app, 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, seeCreate and Run a Simple App Using App Designer

金宝appsimulink.compiler.example.lorenzsystem

应用详细信息

Open theLorenzsystemAppmlappfile. You can view the code written to create this app in theCode Viewsection of App Sesigner. The essential part of building this app is the behavior of the模拟button. It has the following salient parts: creating the仿真inputobject, 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.

创建Simulink.SimulationInput目的

在功能中创建模拟,定义一个空Simulink.SimulationInputobject for the model. Use thisSimulink.SimulationInputobject to set simulation callbacks and variables for the model.

仿真回调函数用于注册回调。这金宝appsimulink.compiler.setpoststepfcn函数注册一个在每个模拟步骤后调用的回调。这simulink.compiler.setExternalOuputsFcn注册一个回调,该回调在模拟过程中动态处理模型根级别的每个输出端口的值。

Use thesetVariable方法的方法Simulink.SimulationInput对象向应用程序提供参数值。模拟的值是从应用程序UI的编辑字段获得的。要部署应用程序,请使用simulink.compiler.configureForDeployment功能。(评论调用的代码行simulink.compiler.configureForDeployment功能更快的调试速度。)

功能siminp = createSimulationInput(app)%创建一个空模拟对象simInp = Simulink.SimulationInput('LorenzSystemModel');% Specify the simulation callbackssiminp = 金宝appsimulink.compiler.setpoststepfcn(siminp, @app.poststepfcn);siminp = 金宝appsimulink.compiler.setExternalOutputsfcn(siminp, @app.processOutputs);%从UI编辑字段加载参数值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);%配置SIMINP进行部署% DEBUG TIP: Comment out the line below for% faster/easier debugging when runnng in MATLABsimInp = simulink.compiler.configureForDeployment(simInp);end%创建模拟声明

Simulation Callback Functions

仿真回调函数寄回回调,可让您从输出端口读取值并将值写入根输入端口。这些功能在每个仿真时间步骤中注册回调,这使您可以查看模拟的实时结果。

ProcessOutputs打回来

金宝appsimulink.compiler.setexternalOutputsfcn行指该功能procressuputs。这procressuputscallback function processes the values for every root output port block of model during simulation. Theprocressuputs功能is called once per port and per the sample time of the port. When theprocressuputs调用函数,它读取每个根外口块的值并缓存这些值。这后fcnobtains the cached values to update the plot.

功能ProcessOutputs(app, opIdx, ~, data)% Called during sim to process the external output port data,% will be called once per port per its sample hit.转变OPIDX案子1 app.txyzbuffer.x = data;案子2 app.txyzBuffer.y = data;案子3 app.txyzbuffer.z = data;除此以外error(['Invalid port index: ', num2str(opIdx)]);endend

后fcn打回来

后fcncallback function is invoked after every simulation step. The时间argument is the time for the previous simulation step. The后fcn功能obtains the cached outport block values for every时间并将这些值传递给updateTrace在模拟时间绘制缓存值的功能。

功能后fcn(应用程序,时间)% Called during sim after each simulation time stepapp.updateSimStats(time);如果app.status == appstatus.starting app.switchstatus(appstatus.running);app.simstats.wallclocktimewterfirsteptep = tic;end如果app.stopRequested app.switchStatus(AppStatus.Stopping); stopRequestedID = [mfilename('class'),':停止'];投掷(Mexception(Stoprequestedid,,“停止请求”);end%--------------------------------------------------------------app.txyzBuffer.t = time; x = [app.txyzBuffer.x]; y = [app.txyzBuffer.y]; z = [app.txyzBuffer.z]; app.updateTrace(x, y, z); app.updateMarker('头',x,y,z);%--------------------------------------------------------------绘制限制;end% postStepFcn

在应用程序设计师中测试

在部署应用程序之前,请确保应用程序在应用程序设计器中运行。点击模拟to verify that the application works by simulating the model for different values.

编译应用程序进行部署

You can use the App Designer to compile and deploy the app. You can also use the部署功能。有关与应用程序设计师进行编译和部署的更多信息,请参见Develop Apps Using App Designer,Web Apps申请编译器

To compile the app in this example, use theMCC命令随后是应用程序名称。

MCC -M LorenzsystemApp

输入参数

全部收缩

仿真输入和模型的更改用于模拟,指定为Simulink.SimulationInputobject

例子:in = 金宝appsimulink.simulationInput('vdp')

Function handle for a callback that is invoked after every simulation step.

  • 时间- 上一个模拟步骤的时间,由数字值指定。

Version History

在R2020b中引入