金宝appsimulink.compiler.setpoststepfcn
Register a callback to run after each simulation step
描述
in = 金宝appsimulink.compiler.setpoststepfcn(
函数注册一个在每个模拟步骤后调用的回调。在
,@(time) postStepFcn(time))
例子
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 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 theLorenzsystemApp
。mlapp
file. 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仿真input
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.
创建Simulink.SimulationInput
目的
在功能中创建模拟
,定义一个空Simulink.SimulationInput
object for the model. Use thisSimulink.SimulationInput
object 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
。这procressuputs
callback 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
调用函数,它读取每个根外口块的值并缓存这些值。这后fcn
obtains 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
打回来
这后fcn
callback 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
输入参数
在
—Simulation inputs
Simulink.SimulationInput
object
仿真输入和模型的更改用于模拟,指定为Simulink.SimulationInput
object
例子:in = 金宝appsimulink.simulationInput('vdp')
@(time) postStepFcn(time))
—回调的功能句柄
MATLAB®功能handle
Function handle for a callback that is invoked after every simulation step.
时间
- 上一个模拟步骤的时间,由数字值指定。
Version History
matlab命令
You clicked a link that corresponds to this MATLAB command:
通过在MATLAB命令窗口中输入该命令。Web浏览器不支持MATLAB命令。金宝app
选择一个网站
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:。
您还可以从以下列表中选择一个网站:
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.
美洲
- AméricaLatina(Español)
- 加拿大(英语)
- 美国(英语)