Matlab GUI 菜单和工具栏常用操作
PAGE
PAGE 5
Matlab GUI 常用操作
菜单编辑菜单打开GUIDE,选择工具栏上面的菜单编辑按钮
添加菜单回调函数(callback)菜单编辑完成保存后,在fig对应的m文件中会自动生成菜单的回调函数声明例如菜单tag名为menuFilePrint,则对应的callback函数为:function menuFilePrint_Callback(hObject, eventdata, handles)可以在该函数内添加代码实现功能。
菜单项访问获取菜单句柄在matlab的函数中要对某个菜单进行访问,必须获得该菜单的句柄。如果是在菜单的回调函数里面,hObject就是该菜单的句柄;如果是在其他的函数里面要获得某一菜单的句柄,比如选中工具栏上的ZoomIn按钮,按钮处于被按下状态,同时要设置对应菜单项view->Zoom-Zoom In 为选中状态,如下图:
这里要在按钮“ZoomIn”的callback函数里面实现对菜单menuViewZoomIn的状态设置。首先得获得菜单的句柄:% first we must get the handle of menuZoomInhFigChildren = allchild(gcf); % menuView handlehMenuView = findobj(hFigChildren,'Tag','menuView');hvc = allchild(hMenuView); % menuViewZoomhViewZoom = findobj(hvc,'Tag','menuViewZoom');hvzc = allchild(hViewZoom); %menuZoomIn handlehZoomIn = findobj(hvzc,'Tag', 'menuZoomIn');
获取设置菜单状态得到菜单的句柄后就可以得到菜单当前的状态了:if strcmp(get(hZoomIn, 'Checked'),'on') set(hZoomIn, 'Checked', 'off');elseset(hZoomIn, 'Checked', 'on');end
工具栏不显示工具栏GUIDE开发界面在对应的m文件中找到function Main_OpeningFcn(hObject, eventdata, handles, varargin)在该函数里面添加:set(hObject,'Toolbar','none');
M文件开发hMainFigure = figure(... % The main GUI figure 'MenuBar','none', ... 'Toolbar','none', ... 'HandleVisibility','callback', ... 'Color', get(0,... 'defaultuicontrolbackgroundcolor'));
显示“Figure”工具栏set(hObject,'Toolbar','figure');
添加自定义工具栏set(hObject,'Toolbar','none');hToolbar = uitoolbar(hObject);hNewPushtool = uipushtool(... % Open toolbar button 'Parent',hToolbar,... 'TooltipString','New File',... 'CData',imread('New.bmp')); 'HandleVisibility','callback', ... 'ClickedCallback', @hOpenMenuitemCallback);
获取工具栏句柄% get toolbar handlehToolbar = findall(gcf,'Type','uitoolbar');if isempty(hToolbar), return, end
获取figure工具栏按钮的tag值% get toolbar handlehToolbar = findall(gcf,'Type','uitoolbar'