callback function n n n Close Request Fcn
回调函数(callback function) n n n Close. Request. Fcn-关闭窗口时响应函数 Key. Press. Fcn-键盘按下时响应函数 window. Button. Down. Fcn-鼠标按下时响应函数 Window. Button. Motion. Fcn-鼠标移动时响应函数 Create. Fcn和Delete. Fcn-建立和删除对象时响应函 数 Call. Back-对象被选中时响应函数
一旦该对象指定的事件发生,将自动调用 某指定的函数,它可以是matlab文件,也可 以是一组matlab程序。 ? gwin=figure gwin = 1 ? set(gwin, 'windowbuttondownfcn', 'helpdlg(''mouse button down!'')') ? set(gwin, 'keypressfcn', 'helpdlg(''keyboard pressed!'')')
? [f, p]=uigetfile('*. m; *. txt', '请选择一个文件') f= fileprint. m p= E: MATLABR 11work
n 字体设置对话框 h_Font=uisetfont 或 h_Font=uisetfont(h_Text, str. Title) h_Font为字体属性的结构 体;h_Text为要设置的字 符句柄,str. Title为对话框 的标题栏内容。 ? s=uisetfont s = Font. Name: Font. Units: Font. Size: Font. Weight: Font. Angle: '楷体_GB 2312' 'points' 42 'bold' 'normal'
n 警告与错误信息对话框 warndlg和 errordlg函数 二者的显示图标不同。 例: ? h=warndlg({‘error: ’, ‘code 1111. ’}, ‘Warning’) ? h=errordlg({'error: ', 'code 1111. '}, 'Error')
n 通用信息框 msgbox(‘显示信息’, ’标题’, ’图标’) 图标包括:Error、Help、Warn以及Custom,如 果缺省则为None。 ? data=1: 64; data=(data'*data)/64; ? msgbox('This is an example of msgbox!', 'custom ico', 'custom', data, hot(64))
n 例1: h_main=figure(‘name’, ‘a demo of gui design’, ‘menubar’, ‘none’, … 'numbertitle', 'off', 'position', [100 300 100]); h_edit=uicontrol('style', 'edit', 'backgroundcolor', [1 1 1], 'position', [20 20 50 20], . . . 'tag', 'myedit', 'string', '1', 'horizontalalignment', 'left'); h_but 1=uicontrol('style', 'pushbutton', 'position', [20 50 50 20], 'string', 'INC', . . . 'callback', ['v=eval(get(h_edit, ''string'')); ', . . . 'set(h_edit, ''string'', int 2 str(v+1)); ']); h_but 2=uicontrol('style', 'pushbutton', 'position', [80 50 50 20], 'string', 'DEC', . . . 'callback', ['v=eval(get(h_edit, ''string'')); ', 'set(h_edit, ''string'', int 2 str(v-1)); ']);
function gui_counter 1() %gui_counter is a demo of gui design. h_main=figure('name', 'a demo of gui design', 'menubar', 'none', . . . 'numbertitle', 'off', 'position', [100 300 100]); h_edit=uicontrol('style', 'edit', 'backgroundcolor', [1 1 1], 'position', [20 20 50 20], . . . 'tag', 'myedit', 'string', '1', 'horizontalalignment', 'left'); h_but 1=uicontrol('style', 'pushbutton', 'position', [20 50 50 20], 'string', 'INC', . . . 'callback', ['h=findobj(gcf, ''tag'', ''myedit''); ', 'v=eval(get(h, ''string'')); ', . . . 'set(h, ''string'', int 2 str(v+1)); ']); h_but 2=uicontrol('style', 'pushbutton', 'position', [80 50 50 20], 'string', 'DEC', . . . 'callback', ['h=findobj(gcf, ''tag'', ''myedit''); ', 'v=eval(get(h, ''string'')); ', . . . 'set(h, ''string'', int 2 str(v-1)); ']);
n 例2 function gui_demo() %GUI_demo is another demo of GUI design. h_main=figure('units', 'normalized', 'position', [0. 3 0. 5 0. 4], . . . 'name', 'GUI demostration', 'numbertitle', 'off'); h_axis=axes('units', 'normalized', 'position', [0. 3 0. 15 0. 6 0. 7], . . . 'tag', 'axplot', 'xlim', [0 10], 'ylim', [-1 1]); t=0: 0. 1: 10; y=sin(t); line(t, y); bmp 1=imread('1. bmp'); bmp 2=imread('2. bmp'); bmp 3=imread('3. bmp'); bmp 4=imread('4. bmp'); h_1=uicontrol('style', 'pushbutton', 'units', 'normalized', . . . 'position', [0. 1 0. 6 0. 06 0. 1], 'cdata', bmp 1, . . . 'callback', 'zoom on', 'tooltipstring', 'Enable zooming'); h_2=uicontrol('style', 'pushbutton', 'units', 'normalized', . . . 'position', [0. 04 0. 45 0. 06 0. 1], 'cdata', bmp 2, . . . 'callback', 'zoom xon', 'tooltipstring', 'Enable zoom on x-axis only'); h_3=uicontrol('style', 'pushbutton', 'units', 'normalized', . . . 'position', [0. 16 0. 45 0. 06 0. 1], 'cdata', bmp 3, . . . 'callback', 'zoom yon', 'tooltipstring', 'Enable zoom on y-axis only'); h_4=uicontrol('style', 'pushbutton', 'units', 'normalized', . . . 'position', [0. 1 0. 3 0. 06 0. 1], 'cdata', bmp 4, . . . 'callback', 'zoom off', 'tooltipstring', 'Disable zooming');
Ø 热键名称accelerator Ø 背景颜色backgroundcolor Ø 前景颜色foregroundcolor Ø 选中状态checked Ø 使能状态enabled Ø 菜单条位置position Ø 分隔符separator
ctxmenu=uicontextmenu; set(gcf, 'uicontextmenu', ctxmenu); uimenu(ctxmenu, 'label', 'zoom on', 'callback', 'zoom on'); uimenu(ctxmenu, 'label', 'x-axis zoom on', 'callback', 'zoom xon'); uimenu(ctxmenu, 'label', 'y-axis zoom on', 'callback', 'zoom yon'); uimenu(ctxmenu, 'label', 'zoom off', 'callback', 'zoom off'); uimenu(ctxmenu, 'label', 'checked', 'on', 'separator', 'on'); uimenu(ctxmenu, 'label', 'disabled', 'enable', 'off');
Tag=rdradio 1 Tag=rdradio 2 Tag=rdradio 3 Tag=lstcolor h 1 = line([0, 1], -0. 09*[1 1]); set(h 1, 'linewidth', 5); h 2 = line([0, 1], 1. 09*[1 1]); set(h 2, 'linewidth', 7); uu={[0 1 0], 0. 05 0. 5}; set(h_main, 'userdata', uu);
set(h_mark, 'color', uu{1}); function exec_bouncing(x) for i=1: length(xx_l) k=1; uu=get(gcf, 'userdata'); x 0=xx_l(i); if nargin==0, y 0=yy_l(i); [x, y, k]=ginput(1); uu{3}=x; set(gcf, 'userdata', uu); set(h_mark, ‘xdata’, x 0, ‘ydata’, … end y 0); pause(uu{2}); if k==1 end if x<0, x=0. 1; end if x>1, x=1; end xx=[0]; yy=[1]; x 0=0; x 1=x; y 1=1; while(x 1<=1+x) xx=[xx x 1]; y 1=~y 1; yy=[yy y 1]; x 0=x 1; x 1=x 1+x; end dx=1/50; xx_l=[0: dx: 1, xx]; xx_l=sort(xx_l); yy_l=interp 1(xx, yy, xx_l); h_mark=line([0], [1]); set(h_mark, 'Marker', 'o', 'markersize', 8, 'color''g', . . . 'tag', 'h_ball', 'linewidth', 5);
function set_speed(key) if key<=3 uu=get(gcf, 'userdata'); hh=gco; switch key h 1=findobj(gcf, 'tag', 'rdradio 1'); set(h 1, 'value', 0) case 1 ; uu{2}=uu{2}*0. 5; h 2=findobj(gcf, 'tag', 'rdradio 2'); set(h 2, 'value', 0) case 2 ; uu{2}=uu{2}*2; h 3=findobj(gcf, 'tag', 'rdradio 3'); set(h 3, 'value', 0) case 3 ; uu{2}=0. 05; set(hh, 'value', 1); case 4 end kk=get(findobj(gcf, 'tag', 'lstcolor'), 'value'); set(gcf, 'userdata', uu); switch kk exec_bouncing(uu{3}); case 1 uu{1}=[1 0 0]; case 2 uu{1}=[0 1 0]; case 3 uu{1}=[0 0 1]; end
- Slides: 36