两个单独的GUI之间的通信

问题描述 投票:1回答:2

因此,我弄清楚了如何从另一个GUI调用一个gui并通过varargin和varargout发送来回信息。 但是,现在我处在两种情况下(没有一个被另一个调用),我相信如果我想在它们之间进行通信,我还需要其他方法。

更确切地说,我正在制作两个与Simulink交互的GUI。 打开模型时会打开一个GUI,并会跟踪信息。 双击一个块时,另一个GUI将打开。 我想将信息从此GUI发送到信息跟踪GUI。

因此,通过搜索,我可以通过在信息跟踪GUI中使用Listener来完成此任务。 或者我可以使用setappdata / getappdatafindall(0,...)直接在信息跟踪GUI中修改变量。

到目前为止,我的尝试还没有成功,我想知道我是否正在采用写方法。 有人可以指出我的方向吗? 让我知道是否可以提供更多信息!

matlab user-interface simulink matlab-figure matlab-guide
2个回答
0
投票

对于此类事情,我一直都使用setappdata / getappdata方法。 这是您所做工作的一般分解。 创建图形时,给它们一个像这样的标签:

figure( ..., 'Tag', 'info_gui', ... ); % tag name is up to you
figure( ..., 'Tag', 'other_gui', ... ); % tag name is up to you

每当您需要一个或多个其他图形的句柄时,只需像这样调用findobj

info_gui_handle = findobj('tag','info_gui');
other_gui_handle = findobj('tag','other_gui');

现在,让我们将一些示例数据存储在info_gui中,稍后我们将进行更新

info_gui_data.x = 1;
info_gui_data.y = 1;
setappdata( info_gui_handle, 'info_gui_data', info_gui_data);

设置好数字后,您可以执行以下操作:

% First you get a handle to the info gui figure

info_gui_handle = findobj('tag','info_gui');

% Next you get the appdata thats stored in this figure.  In this example
% I have previously stored a struct variable called 
% 'info_gui_data' inside the appdata of the info_gui

info_gui_data = getappdata(info_gui_handle ,'info_gui_data');

% Make your changes whatever they are.  Here I am modifying variables x 
% and y that are stored in the struct info_gui_data

info_gui_data.x = 2;
info_gui_data.y = 2;

% Now that I've made changes to the local variable info_gui_data I can 
% now store it back into the info gui's appdata.

setappdata(info_gui_handle ,'info_gui_data',info_gui_data);

我喜欢将所有图形应用程序数据存储在一个巨型结构中。 似乎更容易跟踪,但取决于您。 希望这可以帮助 :)


0
投票

您也可以尝试使用guidataguihandles

假设GUI1的句柄是H1。 在GUI1中,当您想要存储以后可以检索的数据时,请使用:

guidata(H1,data)

在GUI2中,当您需要数据时,请使用:

data = guidata(H1);

或者,您可以将数据存储在uicontrol对象的“用户数据”属性中。 确保将属性“标签”设置为有效的值(例如“ mybutton”)。 要从GUI2访问此文件,请使用:

handles = guihandles(H1);
data = get(handles.mybutton,'UserData');
© www.soinside.com 2019 - 2024. All rights reserved.