GUI间的数据传递机制

    在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的例子:

 

    获得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 data
objects, the directories containing the definitions for the classes
of those objects must be on the MATLAB path when you open the
model 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的例子:

GUIDE中获取Application Data的值,增加新的field并保存的例子

 

 

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的例子

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值