1、前记:实时更新Simulink的数据并在GUI中显示出来,属于GUI与Simulink中的数据交换问题。
多次搜索还是在MATLAB answers中找到了一种解决方式。
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
扫描二维码关注公众号,回复:
13132640 查看本文章

(4)GUI中的控件
(5)结果
后记:将Simulink中scope中的数据实时的显示在GUI中的axes上可以参考: