[英]How to determine if mouse is over axes using MATLAB GUIDE
我知道這個問題以前曾被問過,但我找不到任何好的答案。 我一直在WindowButtonMotionFcn
地使用WindowButtonMotionFcn
,但是我不太了解如何使用它。 在我的程序中,我希望僅當用戶在特定軸上方時才能單擊並存儲坐標,以便普通鼠標出現在GUI的其余部分,並且它們可以與其他按鈕一起玩。 感謝您的見解。
我建議不要使用WindowButtonMotionFcn
,而應使用軸對象的ButtonDownFcn
。 這樣,MATLAB會為您處理命中檢測。
例如:
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
每次在軸內單擊時,都會打印坐標,但在其他位置單擊時,則不執行任何操作。
編輯:
由於這是GUIDE GUI,所以最簡單的方法是利用getappdata
在GUI周圍傳遞數據。 首先,您需要將GUI_OpeningFcn
修改為以下內容:
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);
然后在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
然后,您可以使用相同的getappdata
調用將數組拉入任何其他回調。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.