如何将数据从Simulink块获取到MATLAB GUI? [英] How do I get data from a Simulink block into a MATLAB GUI?

查看:682
本文介绍了如何将数据从Simulink块获取到MATLAB GUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Simulink模型,该模型将嵌入式MATLAB函数用于一个块,而且我还无法弄清楚如何在嵌入式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 (i.e. while the model is running). I tried to implement a "to workspace" block in my model but I don't know how to correctly use it.

有人知道如何将数据从Simulink块实时移动到GUI中吗?

Does anyone know how to move data from a Simulink block into a GUI in real-time?

推荐答案

非实时解决方案:

如果要在GUI中设置参数,使用这些参数模拟模型,然后在GUI中显示模拟输出,请在 SIMSET 函数定义Simulink模型与之交互的工作空间.您应该能够取代基本工作空间,以便将数据发送到调用Simulink模型的GUI函数的工作区,或从中进行发送.

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. One solution they describe is using the SIMSET function to define which workspace the Simulink model interacts with. 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.

实时解决方案

根据 MikeT 的建议,您可以使用 get_param 函数从代码块获取RuntimeObject:

As suggested by MikeT, you can use a RuntimeObject. You first have to use the get_param function to get the RuntimeObject from the block:

rto = get_param(obj,'RuntimeObject');

其中obj是块路径名或块对象句柄.您可以使用 GCB 功能(在这种情况下,您可以将obj替换为gcb).然后,您可以使用以下命令获取块的输出:

Where obj is either a block pathname or a block-object handle. You can get the pathname of the most recently selected block using the GCB function (in which case you can replace obj with gcb). You can then get the block's output with the following:

blockData = rto.OutputPort(1).Data

文档中的另一个警告:

为确保数据"字段包含 正确的块输出,关闭 信号存储复用选项(请参见 信号存储重用) 优化参数对话框中的优化窗格.

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.

您可能最终会在GUI中运行循环或计时器例程,只要仿真正在运行,它们就会不断从RuntimeObject获取输出数据.该文档还指出:

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. The documentation also states:

运行时对象仅在 包含块的模型是 运行或暂停.如果模型是 已停止,get_param返回一个空 处理.当您停止或暂停 模型,所有现有的手柄 运行时对象变为空.

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. When you stop or pause a model, all existing handles for run-time objects become empty.

因此,您的循环或计时器例程必须首先检查RuntimeObject是否存在,然后停止(如果不存在)或从中获取数据(如果存在).我不确定确切地如何检查RuntimeObject是否存在,但是我相信您可以检查该对象是否为空或

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). 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:

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

这篇关于如何将数据从Simulink块获取到MATLAB GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆