简体   繁体   中英

How to determine if mouse is over axes using MATLAB GUIDE

I know this question has been asked before, but I can't find any good answers. I keep stumbling upon WindowButtonMotionFcn , but I don't really understand how to use it. In my program I want to be able to click and store coordinates ONLY when the user is above a certain axes, so that the normal mouse appears for the rest of the GUI and they can play with other buttons. Thanks for any insight.

I would recommend not using WindowButtonMotionFcn and instead use the ButtonDownFcn of your axes object. This way MATLAB takes care of your hit detection for you.

For example:

function testcode()
h.myfig = figure;
h.myaxes = axes( ...
    'Parent', h.myfig, ...
    'Units', 'Normalized', ...
    'Position', [0.5 0.1 0.4 0.8], ...
    'ButtonDownFcn', @myclick ...
    );
end

function myclick(~, eventdata)
fprintf('X: %f Y: %f Z: %f\n', eventdata.IntersectionPoint);
% Insert data capture & storage here
end

Prints your coordinate every time you click inside the axes but does nothing when you click anywhere else.

EDIT:

Since this is a GUIDE GUI the easiest approach is to utilize getappdata to pass data around the GUI. To start, you need to modify your GUI_OpeningFcn to something like the following:

function testgui_OpeningFcn(hObject, eventdata, handles, varargin)

% Choose default command line output for testgui
handles.output = hObject;

% Initialize axes click behavior and data storage
set(handles.axes1, 'ButtonDownFcn', {@clickdisplay, handles}); % Set the axes click handling to the clickdisplay function and pass the handles
mydata.clickcoordinates = []; % Initialize data array
setappdata(handles.figure1, 'mydata', mydata); % Save data array to main figure

% Update handles structure
guidata(hObject, handles);

And then add a click handling function elsewhere in your GUI:

function clickdisplay(~, eventdata, handles)
mydata = getappdata(handles.figure1, 'mydata'); % Pull data from main figure
mydata.clickcoordinates = vertcat(mydata.clickcoordinates, eventdata.IntersectionPoint); % Add coordinates onto the end of existing array
setappdata(handles.figure1, 'mydata', mydata); % Save data back to main figure

You can then pull the array into any other callback using the same getappdata call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM