声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2147|回复: 6

[GUI界面] 回调编程小结

[复制链接]
发表于 2007-7-1 15:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
最近在看回调编程,算是自己的读书笔记吧。拿来分享一下,如果已经有人发过相同的帖子。就请版主把删了吧
1 回调编程小节
函数回调编程的难点在于获取数据,也就是数据的传递。下面对几种常见的数据传递方式做下总结:
(1)将数据变量声明成persistent类型。
(2)使用GUI图形的应用属性保存数据:setappdata(Hf,'MyGuiFcnData',mydata)%保存在图像句柄Hf的标签属     性MyGuiFcnData中;读取时:
     Hf=findobj('Tag','MyGuiFcnData');%查找图形句柄
     mydata=getappdata(Hf,'MyGuiFcnData')%读取数据
(3)使用guidata将数据保存在GUI图形的ApplicationData属性中
     guidata(Hf,mydata);%保存
     mydata=guidata(gcbo);%读取
     gcbo函数返回调用回调的对象的句柄。该种方式在后台用的仍然是(2)中的函数来实现,不过更简洁。
(4)使用附加变量参数
     我自己认为是最简便的一种方式
举个简单的例子:
function myguifcn
%MYGUIGCN Sample GUI Function Using Function Callback
%code that creates the GUI and sets the callback
h0=figure('toolbar','none','position',[200 150 350 300],...
         'name','演示窗口','NumberTitle','off',...
         'menubar','none');
h1=axes('parent',h0,...
    'position',[0.15 0.2 0.75 0.75],...
    'visible','on');
data=peaks(20);
meshc(data);
p1=uicontrol('Parent',h0,'Style',...
          'PushButton','String',...
           'mesh','Callback',{@meshfcn,data},...
           'position',[50 20 50 20]);
p2=uicontrol('Parent',h0,'Style',...
          'PushButton','String',...
           'surf','Callback',{@surffcn,data},...
           'position',[150 20 50 20]);
p3=uicontrol('Parent',h0,'Style',...
          'PushButton','String',...
           'contour','Callback',{@contourfcn,data},...
           'position',[250 20 50 20]);
%-------------------------------------------------
function meshfcn(cbo,eventdata,data)
mesh(data);
%-------------------------------------------------
function surffcn(cbo,eventdata,data)
surf(data);
%-------------------------------------------------
function contourfcn(cbo,eventdata,data)
contour(data);
回调使用函数句柄,数据由回调函数附加参量来传递。这个例子只是为了说明函数句柄用于回调和附加参量传递数据的办法,对于简单的情形可以直接在把语句写到callback里。这个例子也简写为:
function myguifcn
%MYGUIGCN Sample GUI Function Using Function Callback
%code that creates the GUI and sets the callback
h0=figure('toolbar','none','position',[200 150 350 300],...
         'name','演示窗口','NumberTitle','off',...
         'menubar','none');
h1=axes('parent',h0,...
    'position',[0.15 0.2 0.75 0.75],...
    'visible','on');
data=peaks(20);
meshc(data);
p1=uicontrol('Parent',h0,'Style',...
          'PushButton','String',...
           'mesh','Callback','mesh(data)',...
           'position',[50 20 50 20]);
p2=uicontrol('Parent',h0,'Style',...
          'PushButton','String',...
           'surf','Callback','surf(data)',...
           'position',[150 20 50 20]);
p3=uicontrol('Parent',h0,'Style',...
          'PushButton','String',...
           'contour','Callback','contour(data)',...
           'position',[250 20 50 20]);

评分

1

查看全部评分

回复
分享到:

使用道具 举报

 楼主| 发表于 2007-7-1 15:41 | 显示全部楼层
谢谢版主的支持,原创的内容不多。方法都是精通matlab7.0一书上提到了。我只是把里边的方法用程序验证了一下,希望对刚入门的有些帮助
发表于 2007-7-2 09:55 | 显示全部楼层
支持支持,我也正学GUI,但还没摸着门道,多分享你的学习成果吧.
发表于 2007-7-2 10:21 | 显示全部楼层

回复 #2 花如月 的帖子

:lol 我来给你顶一下!呵呵!!
 楼主| 发表于 2007-7-2 10:51 | 显示全部楼层
前三种方式的实现程序:
function myguifcn
%MYGUIGCN Sample GUI Function Using Function Callback
%code that creates the GUI and sets the callback
global data
%persistent data
h0=figure('toolbar','none','position',[200 150 350 300],...
         'name','演示窗口','NumberTitle','off',...
         'menubar','none','color',[0.5 0 0.8]);
h1=axes('parent',h0,...
    'position',[0.15 0.2 0.75 0.75],...
    'visible','on');
data=peaks(20);
setappdata(h0,'MyGuiData',data);%保存数据在h0的‘Tag’属性里
guidata(h0,data);%使用guidata函数保存用户数据
meshc(data);
p1=uicontrol('Parent',h0,'Style',...
          'PushButton','String',...
           'mesh','Callback',@meshfcn,...
           'position',[50 20 50 20]);
p2=uicontrol('Parent',h0,'Style',...
          'PushButton','String',...
           'surf','Callback',@surffcn,...
           'position',[150 20 50 20]);
p3=uicontrol('Parent',h0,'Style',...
          'PushButton','String',...
           'contour','Callback',@contourfcn,...
           'position',[250 20 50 20]);
%-------------------------------------------------
function meshfcn(cbo,eventdata)
%persistent data
global data
%data=guidata(cbo);
%data=getappdata(gcf,'MyGuiData');
mesh(data);
%-------------------------------------------------
function surffcn(cbo,eventdata)
%persistent data
global data
%data=guidata(cbo);
%data=getappdata(gcf,'MyGuiData');
surf(data);
%-------------------------------------------------
function contourfcn(cbo,eventdata)
%persistent data
global data%全局变量方式
%data=guidata(cbo);%guidata函数
%data=getappdata(gcf,'MyGuiData');%图形句柄方式
contour(data);
在使用全局变量的时候,在精通matlab一书中讲的用persistent声明。在8兄关于gui心得的精华帖子里提到用global,我在调试的过程中发现用global声明的变量确实可以传递数据,程序一切正常。但是用persistent时,没有绘制出图形(也没报任何错误)。现在也没找出问题所在,第一次写包含子函数的函数m文件。希望高手多批评指正!对4楼的支持深表感谢。

[ 本帖最后由 花如月 于 2007-7-2 10:54 编辑 ]

评分

1

查看全部评分

发表于 2007-7-2 13:04 | 显示全部楼层
原帖由 花如月 于 2007-7-2 10:51 发表
前三种方式的实现程序:
function myguifcn
%MYGUIGCN Sample GUI Function Using Function Callback
%code that creates the GUI and sets the callback
global data
%persistent data
h0=figure('toolb ...


没用过 persistent,而且准备搬家离校了,所以没有时间细看,呵呵。看有否其他高手路过了。。。。。。。
 楼主| 发表于 2007-7-2 13:55 | 显示全部楼层

回复 #6 eight 的帖子

谢谢8兄这段时间的指导和栽培,衷心祝你工作顺利,万事如意!还有就是用过persistent声明全局变量的帮我看看5楼的程序为什么不能正常传递。
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-6-2 01:39 , Processed in 0.061762 second(s), 19 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表