You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Matlab App Designer中通过独立图形窗口获取鼠标点击点

Solution: Open Standalone Figure Window in App Designer for Interactive Graphics & Mouse Clicks

Absolutely, you can totally create a standalone, non-UI figure window in MATLAB App Designer to display graphics and capture mouse click positions—let’s sort out that blank window issue you’re facing right now.

The Core Issue

Your current code is probably creating the blank standalone figure but not directing your plot to it; instead, the graph is defaulting to App Designer’s built-in UI axes. The fix is straightforward: explicitly create a traditional figure() window, then draw your graphics onto that window, and attach the mouse click callback to it.

Step-by-Step Code Example

Here’s how to implement this in your App Designer app:

  1. First, add a property to your app (go to the "Code View" > "Properties" tab) to store the standalone figure—this prevents it from being automatically garbage-collected:
properties (Access = private)
    StandaloneFig % Stores the standalone figure handle
end
  1. Add a button callback (or trigger this logic from any UI event) to create the figure, plot your data, and set up the mouse click handler:
function OpenInteractivePlotButtonPushed(app, event)
    % Create a standalone figure window (not a uifigure)
    app.StandaloneFig = figure(...
        'Name', 'Interactive Plot Window', ...
        'NumberTitle', 'off', ...
        'Position', [100 100 800 600]); % Set window position/size

    % Plot your data DIRECTLY onto this standalone figure
    x = linspace(0, 2*pi, 200);
    y = sin(x) + 0.1*randn(size(x)); % Example data
    ax = axes(app.StandaloneFig); % Get the axes of the standalone figure
    plot(ax, x, y, 'LineWidth', 1.5);
    xlabel(ax, 'X Axis');
    ylabel(ax, 'Y Axis');
    title(ax, 'Click Anywhere to Capture Coordinates');

    % Attach the mouse click callback to the figure
    set(app.StandaloneFig, 'WindowButtonDownFcn', @(src, evt) app.CaptureMouseClick(src, evt));
end
  1. Create the mouse click callback function to handle clicks and retrieve coordinates:
function CaptureMouseClick(app, src, evt)
    % Get the exact click coordinates from the figure
    clickPoint = get(src, 'CurrentPoint');
    xClick = clickPoint(1, 1);
    yClick = clickPoint(1, 2);

    % Option 1: Display coordinates in your App Designer UI (e.g., a text field)
    app.ClickCoordinatesEditField.Value = sprintf('Clicked at (%.2f, %.2f)', xClick, yClick);

    % Option 2: Mark the click position on the standalone plot
    ax = src.CurrentAxes;
    hold(ax, 'on');
    plot(ax, xClick, yClick, 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r');
    hold(ax, 'off');
end

Key Fixes Explained

  • By creating a figure() (not uifigure()), you get the traditional MATLAB graphics window that supports full interactive operations.
  • Explicitly specifying the figure/axes as the parent for your plot() command ensures your graph renders in the standalone window, not the App Designer UI.
  • Storing the figure handle in an app property keeps the window open and accessible for callbacks.

Bonus Tips

  • If you want to handle window closure cleanly, add a CloseRequestFcn to the figure to reset the app property:
    set(app.StandaloneFig, 'CloseRequestFcn', @(src, evt) app.CloseStandaloneFig(src, evt));
    
    Then define the closure callback:
    function CloseStandaloneFig(app, src, evt)
        delete(src);
        app.StandaloneFig = [];
    end
    
  • Avoid mixing UI axes and standalone figure axes—always specify the parent object for plots to prevent unexpected behavior.

内容的提问来源于stack exchange,提问作者Qiana Curcuru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:45:12