简体   繁体   English

如何从Simulink块获取数据到MATLAB GUI?

[英]How do I get data from a Simulink block into a MATLAB GUI?

I have a Simulink model that uses an embedded MATLAB function for a block, and I haven't been able to figure out how to move data between the embedded MATLAB block and a GUI in real-time (ie while the model is running). 我有一个Simulink模型,该模型将嵌入式MATLAB函数用于一个块,但是我一直无法弄清楚如何在嵌入式MATLAB块和GUI之间实时(即在模型运行时)移动数据。 I tried to implement a "to workspace" block in my model but I don't know how to correctly use it. 我试图在模型中实现“到工作区”块,但我不知道如何正确使用它。

Does anyone know how to move data from a Simulink block into a GUI in real-time? 有谁知道如何将数据从Simulink块实时移动到GUI?

Non-real-time solution: 非实时解决方案:

If you want to set parameters in a GUI, simulate a model with those parameters, and then display the simulation output in the GUI, there is a good tutorial on blinkdagger.com . 如果要在GUI中设置参数,使用这些参数模拟模型,然后在GUI中显示模拟输出,则blinkdagger.com上有一个很好的教程。 One solution they describe is using the SIMSET function to define which workspace the Simulink model interacts with. 他们描述的一种解决方案是使用SIMSET函数来定义Simulink模型与之交互的工作空间。 You should be able to supersede the base workspace so that data is instead sent to and from the workspace of the GUI functions that are calling the Simulink model. 您应该能够取代基本工作区,以便将数据发送到正在调用Simulink模型的GUI函数的工作区,或从中发送数据。

Real-time solution 实时解决方案

As suggested by MikeT , you can use a RuntimeObject . 根据MikeT的建议,可以使用RuntimeObject You first have to use the get_param function to get the RuntimeObject from the block: 首先,您必须使用get_param函数从该块获取RuntimeObject:

rto = get_param(obj,'RuntimeObject');

Where obj is either a block pathname or a block-object handle. 其中obj是块路径名或块对象句柄。 You can get the pathname of the most recently selected block using the GCB function (in which case you can replace obj with gcb ). 您可以使用GCB函数获取最近选择的块的路径名(在这种情况下,可以将obj替换为gcb )。 You can then get the block's output with the following: 然后,您可以使用以下命令获取块的输出:

blockData = rto.OutputPort(1).Data

One additional caveat from the documentation: 文档中的另一项警告:

To ensure the Data field contains the correct block output, turn off the Signal storage reuse option (see Signal storage reuse) on the Optimization pane in the Configuration Parameters dialog box. 为确保“数据”字段包含正确的块输出,请在“ 配置参数”对话框的“ 优化”窗格上关闭“ 信号存储重用”选项(请参阅信号存储重用)。

You would likely end up with a loop or a timer routine running in your GUI that would continuously get the output data from the RuntimeObject for as long as the simulation is running. 您可能最终会在GUI中运行循环或计时器例程,只要仿真正在运行,它们便会不断从RuntimeObject获取输出数据。 The documentation also states: 该文档还指出:

A run-time object exists only while the model containing the block is running or paused. 仅当包含该块的模型正在运行或暂停时,运行时对象才存在。 If the model is stopped, get_param returns an empty handle. 如果模型已停止,则get_param返回空句柄。 When you stop or pause a model, all existing handles for run-time objects become empty. 停止或暂停模型时,运行时对象的所有现有句柄将变为空。

Your loop or timer routine would thus have to keep checking first that the RuntimeObject exists, and either stop (if it doesn't) or get the data from it (if it does). 因此,您的循环或计时器例程必须先检查RuntimeObject是否存在,然后停止(如果不存在)或从中获取数据(如果存在)。 I'm unsure of exactly how to check for existence of a RuntimeObject, but I believe you would either check if the object is empty or if the BlockHandle property of the object is empty: 我不确定确切如何检查RuntimeObject的存在,但我相信您可以检查对象是否为空或对象的BlockHandle属性是否为空:

isempty(rto)  % Check if the RuntimeObject is empty
%OR
isempty(rto.BlockHandle)  % Check if the BlockHandle property is empty

From your responses, I'm guessing you want to see the results while the simulation is running, is that correct? 根据您的回答,我猜您想在模拟运行时查看结果,对吗? The blinkdagger.com tutorial lets you view the results of a simulation after it is done, but not while it is running. blinkdagger.com教程可让您在仿真完成后(而不是在运行时)查看仿真结果。 Do you basically want to embed something like a scope block into your GUI? 您是否基本上想在您的GUI中嵌入诸如范围块之类的东西?

There's a few ways to do this, the best is probably using the EML block's runtime object . 有几种方法可以做到这一点,最好的方法是使用EML块的runtime对象 If you use this, you should be able to look at the output of the EML block while it is running. 如果使用此功能,则应该能够在EML块运行时查看其输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM