GUI間的資料傳遞機制

來源:互聯網
上載者:User

    在GUI中傳遞資料有以下幾種形式:

 

1.輸入與輸出

   一般guide建立的m檔案的頂層函數的原型一般為:varargout = guide_toolpalette(varargin)。

   其中varargin和varargout分別是變長度的輸入輸出資料行表。通過他們可以完成一些資料的傳遞:輸入varargin可以將資料傳遞給新的GUIs,假如傳遞的是原GUI的figure控制代碼,那麼通過 hand=guidata(h),其中h是傳遞過來的figure控制代碼,那麼在新的GUI中就可以得到原來GUI的handles結構體,從而可以得知原GUI的很多資料;輸出varargin可以將新GUI的一些資料返回給調用它的GUI

 

2.嵌套函數(nested function)

   這個不熟,暫不予以討論。

 

3.UserData

   UserData其實是GUI組件(例如figure、pushbutton等等)的一個屬性,在這個屬性中可以儲存一個變數(通過set函數),通常設計成結構體的形式,方便以field的形式添加資料。要想獲得UserData中儲存的資料,要使用get函數。

   儲存UserData的例子:

function mygui_edittext1(hObject, eventdata, handles)<br />mystring = get(hObject,'String');<br />set(hObject,'UserData',mystring); 

    獲得UserData中儲存資料的例子:

function mygui_pushbutton1(hObject, eventdata, handles)<br />string = get(handles.edittext1,'UserData'); 

 

偶然間發現了一個有趣的東西,simulink的block也有UserData的屬性,help文檔如下:

 

Associating User Data with Blocks

You can use the set_param command
to associate your own data with a block. For example, the following
command associates the value of the variable mydata with
the currently selected block.

set_param(gcb, 'UserData', mydata)

The value of mydata can be any MATLAB data
type, including arrays, structures, objects, and Simulink data
objects.

Use get_param to retrieve
the user data associated with a block.

get_param(gcb, 'UserData')

The following command saves the user data associated with a
block in the model file of the model containing the block.

set_param(gcb, 'UserDataPersistent', 'on');
Note  If persistent UserData for a block contains any Simulink dataobjects, the directories containing the definitions for the classesof those objects must be on the MATLAB path when you open themodel containing the block.

 

 

4.Application Data(appdata)

  通過setappdata(h,'name',value)、value = getappdata(h,name)和values = getappdata(h)、isappdata(h,name)以及rmappdata(h,name)這四個API函數,可以為GUI組件添加appdata、恢複資料、測試appdata和刪除appdata。只有圖形控制代碼中才有Application Data。

   和UserData不同的是,每個組件的appdata都可以存若干個任何類型的變數,變數名是name,h是控制代碼,value是要儲存的資料。

   appdata和UserData的不同點:

  • 可以給Application Data assign多個value,而只能給UserData assign一個value;
  • 引用Application Data要藉助name,而引用UserData則像引用其他屬性的值一樣(用get函數);

GUIDE中建立Application Data的例子:

function mygui_OpeningFcn(hObject, eventdata, handles, varargin)<br />matrices.rand_35 = randn(35);<br />setappdata(hObject,'mydata',matrices);

GUIDE中擷取Application Data的值,增加新的field並儲存的例子

function mygui_pushbutton1(hObject, eventdata, handles)<br />%擷取Application data的值<br />matrices = getappdata(handles.figure1,'mydata');<br />%增加新的field<br />matrices.randn_50 = randn(50);<br />%儲存更改後的Application data<br />setappdata(handles.figure1,'mydata',matrices);  

 

5.GUI data(guidata)

   在GUIDE建立的m檔案中,大多數的函數原型為:function varargout = guide_toolpalette_OutputFcn(hObject, eventdata, handles)或者function guide_toolpalette_OpeningFcn(hObject, eventdata, handles, varargin)。可見每種都有一個參數handles參數。這個參數是一個和figure控制代碼關聯的結構體,該結構體中儲存著該GUI中每個組件的控制代碼,例如假如GUI中有個pushbutton的Tag為button1,則在handles結構體中必有一個field為button1,且handles.button1即為該pushbutton的控制代碼。

   guidata(object_handle,data)可以用data更新原來的GUI data,否則修改不會起作用。

  data = guidata(object_handle)可以得到與object_handle控制代碼關聯的GUI data。

 

   要注意,每個GUI(也可以說是每個figure)只能關聯一個GUI data。所以一般設計成結構體變數,方便添加資料。在GUIDE建立的M檔案中,GUI data被自動設計成了handles結構體

 

  一個關於GUI data的例子

function toolPalette_SelectionChangeFcn(hObject, eventdata, handles)<br />%更改handles結構體(appdata)中的一個域的值<br />handles.mCurrentTool = hObject;<br />%儲存修改,更新handles結構體(Appdata)<br />guidata(hObject, handles); 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.