1、1Matlab GUI 常用操作菜单1. 编辑菜单打开 GUIDE,选择工具栏上面的菜单编辑按钮2. 添加菜单回调函数(callback)菜单编辑完成保存后,在 fig 对应的 m 文件中会自动生成菜单的回调函数声明例如菜单 tag 名为 menuFilePrint,则对应的 callback 函数为:function menuFilePrint_Callback(hObject, eventdata, handles)可以在该函数内添加代码实现功能。3. 菜单项访问 获取菜单句柄在 matlab 的函数中要对某个菜单进行访问,必须获得该菜单的句柄。如果是在菜单的回调函数里面,hObject
2、就是该菜单的句柄;2如果是在其他的函数里面要获得某一菜单的句柄,比如选中工具栏上的 ZoomIn 按钮,按钮处于被按下状态,同时要设置对应菜单项 view-Zoom-Zoom In 为选中状态,如下图:这里要在按钮“ZoomIn”的 callback 函数里面实现对菜单 menuViewZoomIn 的状态设置。首先得获得菜单的句柄:% first we must get the handle of menuZoomInhFigChildren = allchild(gcf);% menuView handlehMenuView = findobj(hFigChildren,Tag,menuV
3、iew);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);end3工
4、具栏1. 不显示工具栏1) GUIDE 开发界面在对应的 m 文件中找到function Main_OpeningFcn(hObject, eventdata, handles, varargin)在该函数里面添加:set(hObject,Toolbar,none);2) M 文件开发hMainFigure = figure(. % The main GUI figureMenuBar,none, .Toolbar,none, .HandleVisibility,callback, .Color, get(0,.defaultuicontrolbackgroundcolor);2. 显示“Fi
5、gure”工具栏set(hObject,Toolbar,figure);3. 添加自定义工具栏set(hObject,Toolbar,none);hToolbar = uitoolbar(hObject);hNewPushtool = uipushtool(. % Open toolbar buttonParent,hToolbar,.TooltipString,New File,.CData,imread(New.bmp);HandleVisibility,callback, .ClickedCallback, hOpenMenuitemCallback);4. 获取工具栏句柄% get t
6、oolbar handlehToolbar = findall(gcf,Type,uitoolbar);if isempty(hToolbar), return, end5. 获取 figure 工具栏按钮的 tag 值% get toolbar handlehToolbar = findall(gcf,Type,uitoolbar);if isempty(hToolbar), return, end4% get all childrenhChildren = allchild(hToolbar);if isempty(hChildren), return,endhTags = get(hCh
7、ildren, Tag);hTags 保存了工具栏所有按钮的 tag 值:6. 获取设置 figure 工具栏按钮的 callback 函数%reset callback function for “Zoom in“hZoomIn = findobj(hChildren,Tag,Exploration.ZoomIn);set(hZoomIn,ClickedCallback,tbZoomIn_Callback);function tbZoomIn_Callback(hObject, eventdata)% hObject handle to tbZoomIn (see GCBO)% eventd
8、ata reserved - to be defined in a future version of MATLAB7. 删除 figure 工具栏按钮% set figure toolbar firstset(hObject,Toolbar,figure);% get toolbar handletbh = findall(hObject,Type,uitoolbar);if isempty(tbh), return, end% toolbar buttons collectionhChildren = allchild(tbh);if isempty(hChildren), return,
9、 end% delete data cursor buttonhDeleted = findobj(hChildren,Tag,Exploration.DataCursor);delete(hDeleted);58. 设置 figure 工具栏按钮的状态% get toolbar handlehToolbar = findall(gcf,Type,uitoolbar);if isempty(hToolbar), return, end% get all childrenhChildren = allchild(hToolbar);if isempty(hChildren), return,endhTags = get(hChildren, Tag);% find “Zoom In“ buttonhb = findobj(hChildren,Tag,Exploration.ZoomIn);if isempty(hb),return, end% set stateif strcmp(get(hObject, Checked),on)set(hObject, Checked, off);set(hb,State, off);elseset(hObject, Checked, on);set(hb,State, on);end