将Simulink中的数据更新到GUI中(记录)

1、前记:实时更新Simulink的数据并在GUI中显示出来,属于GUI与Simulink中的数据交换问题。

     多次搜索还是在MATLAB answers中找到了一种解决方式。

     来源:https://www.mathworks.com/matlabcentral/answers/96425-how-can-i-update-a-gui-with-values-from-my-simulink-model-as-it-is-running-by-using-a-execution-even?s_tid=answers_rc1-1_p1_BOTH

2、主要步骤解释(链接中有例子可以下载仔细研究):

(1)在Simulink中的StartFcn

(2)编写updategui监听函数(监听函数链接https://www.mathworks.com/help/simulink/slref/add_exec_event_listener.html

   参考链接:https://www.mathworks.com/help/simulink/ug/accessing-block-data-during-simulation.html#f13-92463

   监听函数的作用就是在周期内更新被监听模块的数据。

function varargout = updategui(varargin)
%create a run time object that can return the value of the gain block's
%output and then put the value in a string.

%创建一个运行时对象,该对象可以返回增益块的值输出,然后将值放在字符串中。

rto = get_param('mytestmdl/Gain','RuntimeObject');
rto1 = get_param('mytestmdl/Gain1','RuntimeObject');%获得simulink中的gain

str = num2str(rto.OutputPort(1).Data);
str1 = num2str(rto1.OutputPort(1).Data);

%更新gui,将得到的gian输出值显示到Tag为currState的窗口上
%get a handle to the GUI's 'current state' window
statestxt = findobj('Tag','currState');%currState为显示窗的Tag
statestxt1 = findobj('Tag','edit4');
plotaxi= findobj('Tag','axes1');
aT=[str;str1];
%update the gui
set(statestxt,'string',str);
set(statestxt1,'string',aT);

(3)Simulink模块名为:mytestmdl

(4)GUI中的控件

(5)结果

后记:将Simulink中scope中的数据实时的显示在GUI中的axes上可以参考:

            https://www.jianshu.com/p/389daf78b3ee

猜你喜欢

转载自blog.csdn.net/weixin_39090239/article/details/102167610