首页 > 代码库 > Matlab学习-----------GUIDE菜单学习

Matlab学习-----------GUIDE菜单学习

打开GUIDE,添加组件,然后点击菜单编辑按钮:


编辑菜单和子菜单,包含快捷键,labeltag,然后点击View编辑菜单的回调函数:


为按钮添加回调函数,程序如下:

function varargout = guide_menu(varargin)
% GUIDE_MENU MATLAB code for guide_menu.fig
%      GUIDE_MENU, by itself, creates a new GUIDE_MENU or raises the existing
%      singleton*.
%
%      H = GUIDE_MENU returns the handle to a new GUIDE_MENU or the handle to
%      the existing singleton*.
%
%      GUIDE_MENU('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUIDE_MENU.M with the given input arguments.
%
%      GUIDE_MENU('Property','Value',...) creates a new GUIDE_MENU or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before guide_menu_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to guide_menu_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help guide_menu
% Last Modified by GUIDE v2.5 22-Aug-2014 09:02:40

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @guide_menu_OpeningFcn, ...
                   'gui_OutputFcn',  @guide_menu_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before guide_menu is made visible.
function guide_menu_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to guide_menu (see VARARGIN)
% Choose default command line output for guide_menu
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes guide_menu wait for user response (see UIRESUME)
% uiwait(handles.figure1); 

% --- Outputs from this function are returned to the command line.
function varargout = guide_menu_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;

% --- Executes on button press in sin.
function sin_Callback(hObject, eventdata, handles)
% hObject    handle to sin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x=-pi:0.01:pi;
y=sin(x);
plot(handles.axes1,x,y);

% --- Executes on button press in show.
function show_Callback(hObject, eventdata, handles)
% hObject    handle to show (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axis(handles.axes2);
imshow('pang.tif');   %显示图片,图片位置在程序所在目录
 
% --------------------------------------------------------------------
function mainmenu_Callback(hObject, eventdata, handles)
% hObject    handle to mainmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
 
% --------------------------------------------------------------------
function showall_Callback(hObject, eventdata, handles)
% hObject    handle to showall (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%显示菜单,获取所有要显示的组件的句柄,将其visible设置为on
h=[handles.axes1 handles.sin handles.clear handles.show handles.axes2];
set(h,'visible','on');
set(allchild(handles.axes1),'visible','on');%将原来绘制的图形也显示出来
set(allchild(handles.axes2),'visible','on');
 
% --- Executes on button press in clear.
function clear_Callback(hObject, eventdata, handles)
% hObject    handle to clear (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%清除坐标轴绘制的图形
try
    delete(allchild(handles.axes1));
end
 
 
% --------------------------------------------------------------------
function hideall_Callback(hObject, eventdata, handles)
% hObject    handle to hideall (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%隐藏所有的组件及其子组件
h=[handles.axes1 handles.sin handles.clear handles.show handles.axes2];
set(h,'visible','off');
set(allchild(handles.axes1),'visible','off');
set(allchild(handles.axes2),'visible','off');

结果:

初始界面:


点击主菜单中的显示图片和坐标轴:


绘制图形显示图片:


点击隐藏图片坐标轴菜单: