(1)打开GUIDE,添加一个坐标轴并保存
(2)添加鼠标响应事件:鼠标按下事件,鼠标运动事件,鼠标松开事件
(3)对相应事件编写程序
function varargout = guide_m(varargin) % GUIDE_M MATLAB code for guide_m.fig % GUIDE_M, by itself, creates a new GUIDE_M or raises the existing % singleton*. % % H = GUIDE_M returns the handle to a new GUIDE_M or the handle to % the existing singleton*. % % GUIDE_M('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in GUIDE_M.M with the given input arguments. % % GUIDE_M('Property','Value',...) creates a new GUIDE_M or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before guide_m_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to guide_m_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_m % Last Modified by GUIDE v2.5 25-Aug-2014 09:00:37 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @guide_m_OpeningFcn, ... 'gui_OutputFcn', @guide_m_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_m is made visible. function guide_m_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_m (see VARARGIN) % Choose default command line output for guide_m handles.output = hObject; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %定义两个全局变量 global ButtonDown pos1; ButtonDown =[]; pos1=[]; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Update handles structure guidata(hObject, handles); % UIWAIT makes guide_m wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = guide_m_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 mouse press over figure background, over a disabled or % --- inactive control, or over an axes background. function figure1_WindowButtonDownFcn(hObject, eventdata, handles) % hObject handle to figure1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %鼠标按下事件响应 global ButtonDown pos1; if(strcmp(get(gcf,'SelectionType'),'normal'))%判断鼠标按下的类型,mormal为左键 ButtonDown=1; pos1=get(handles.axes1,'CurrentPoint');%获取坐标轴上鼠标的位置 end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % --- Executes on mouse motion over figure - except title and menu. function figure1_WindowButtonMotionFcn(hObject, eventdata, handles) % hObject handle to figure1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %鼠标运动事件的响应 global ButtonDown pos1; if ButtonDown == 1 pos = get(handles.axes1, 'CurrentPoint');%获取当前位置 line([pos1(1, 1) pos(1, 1)], [pos1(1, 2) pos(1, 2)], 'LineWidth', 4);%划线 pos1 = pos;%更新 end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % --- Executes on mouse press over figure background, over a disabled or % --- inactive control, or over an axes background. function figure1_WindowButtonUpFcn(hObject, eventdata, handles) % hObject handle to figure1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %鼠标按键抬起的响应事件 global ButtonDown; ButtonDown = 0; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%(4)运行测试(按下鼠标,移动鼠标划线)
(5)部分函数方法介绍
Matlab回调函数
SelectionType
Matlab学习-------GUI鼠标事件响应(鼠标划线实例)
原文地址:http://blog.csdn.net/z1137730824/article/details/38816265