【问题标题】:How to determine if mouse is over axes using MATLAB GUIDE如何使用 MATLAB GUIDE 确定鼠标是否在轴上
【发布时间】:2015-11-03 20:22:44
【问题描述】:

我知道以前有人问过这个问题,但我找不到任何好的答案。我一直在偶然发现WindowButtonMotionFcn,但我真的不明白如何使用它。在我的程序中,我希望仅当用户位于某个轴上方时才能够单击和存储坐标,以便普通鼠标出现在 GUI 的其余部分,并且它们可以使用其他按钮。感谢您提供任何见解。

【问题讨论】:

    标签: matlab user-interface matlab-guide axes


    【解决方案1】:

    我建议不要使用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 调用将数组拉入任何其他回调。

    【讨论】:

    • 感谢您的帮助,无论如何您可以稍微解释一下该代码吗?我有点迷路了。另外,在我的 GUI 中,我有一个从点击中获取坐标的函数:感谢您的帮助,无论如何您可以稍微解释一下该代码吗?我有点迷路了。另外,在我的 GUI 中,我有一个从点击中获取坐标的函数:function clickerCall(handles) [handles.markedFrames(handles.markedIndex).x,handles.markedFrames(handles.markedIndex).y] = getpts(handles.axes1) handles.markedIndex = handles.markedIndex+1 disp([handles.markedFrames.x]) updateAxes(handles);,理想情况下它应该只在轴上运行,谢谢!
    • 另外,知道如何将 .IntersectionPoint 中的数据存储到数组中吗?我尝试了类似 x = eventData.IntersectionPoint(1) 无济于事
    • 所以我尝试了这个:handles.output = hObject; handles.axesPos = get(handles.axes1,'Position'); handles.myaxes = axes(... 'Units', 'Normalized', ... 'Position', [handles.axesPos], ... 'ButtonDownFcn', @myclick ... ); 努力在我的 GUI 中使用此功能,但它没有响应任何想法?对大量问题感到抱歉
    • 嘿@excaza,最后一个问题:知道为什么这可以在空白轴上工作,但不能在我用imagesc(movImg,'parent',handles.axes1); 显示图像的轴上工作吗?难道是我用这条线从根本上改变了轴?否则非常感谢你,这真的很有帮助,我也不知道你可以用{ } 传递句柄,所以也谢谢你。
    • imagesc 在坐标区内创建一个图像对象,因此当您单击该对象时,不会触发坐标区命中检测。图像对象也有一个ButtonDownFcn 属性,可以像我们对坐标轴所做的那样定义它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    相关资源
    最近更新 更多