2012-07-23 80 views
1

所以,我想出瞭如何從另一個GUI調用一個GUI並通過varargin和varargout來回發送信息。但是,現在我處於一種情況,我有兩個單獨的guis(一個不從另一個調用),如果我想在它們之間進行通信,我相信我需要其他一些方法。兩個獨立的GUI之間的通信

更確切地說,我製作了兩個與Simulink交互的GUI。打開模型時會打開一個GUI並跟蹤信息。另一個GUI將在雙擊塊時打開。我想從這個GUI發送信息到信息跟蹤GUI。

因此,從我搜索的內容中,我可以通過在信息跟蹤GUI中使用Listener來實現此目的;或者我可以直接使用setappdata/getappdatafindall(0, ...)修改信息跟蹤GUI中的變量。

到目前爲止,我的嘗試還沒有工作,我想知道如果我採取寫方法。有人能指出我的方向嗎?讓我知道我是否可以提供更多信息!

回答

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); 

我喜歡將我的所有圖形appdata存儲在一個巨型結構中。似乎更容易跟蹤,但它取決於你。希望這有助於:)

0

你也可以嘗試使用guidataguihandles

假設GUI1的句柄是H1。在GUI1,當你想存儲以後可以檢索數據,使用方法:

guidata(H1,data) 

在GUI2,當你需要的數據,使用:

data = guidata(H1); 

或者,你可以存儲你的數據在您的uicontrol對象的屬性「用戶數據」。確保你將Property'Tag'設置爲有效的(例如'mybutton')。要從GUI2訪問此,請使用:

handles = guihandles(H1); 
data = get(handles.mybutton,'UserData');