How to get the plot x&y when a user clicks on a point I have plotted

64 ビュー (過去 30 日間)
David Cardinal
David Cardinal 2024 年 1 月 17 日
コメント済み: David Cardinal 2024 年 1 月 17 日
I have some x,y plots of data, that tie back to images that correspond to each value of x.
I'd like my users to be able to click on a data point that I've plotted, providing me with the x,y values so that I can show them the appropriate image.
I'm using an axes within AppDesigner to show the plot. It does seem like I've found a way to get pixel positioning, but since I have hundreds of points, that doesn't seem very useful
Thanks for any help! -- David Cardinal
  1 件のコメント
Mann Baidi
Mann Baidi 2024 年 1 月 17 日
"I'd like my users to be able to click on a data point that I've plotted, providing me with the x,y values"
So you would like to get the xy coordinates when the user clicks on the plot.
"I'm using an axes within AppDesigner to show the plot."
it would be helpful if you can share the code?

サインインしてコメントする。

採用された回答

Avni Agrawal
Avni Agrawal 2024 年 1 月 17 日
Hi David,
I understand you're seeking to extract the X and Y coordinates upon clicking a specific point on your plot within App Designer. To facilitate this functionality, I've outlined a code implementation below that should seamlessly integrate with your existing application.
Please begin by introducing a private property and a corresponding private method to your app class:
properties (Access = private)
lineObj % Handle for the plotted line object
end
methods (Access = private)
% Callback function triggered upon clicking the line
function lineObjButtonDown(app, src, event)
% Retrieve the current point from the UIAxes
point = app.UIAxes.CurrentPoint; % point is a 2x3 array
xClicked = point(1,1);
yClicked = point(1,2);
% Output the coordinates for display or further use
disp(['X: ', num2str(xClicked), ', Y: ', num2str(yClicked)]);
title(app.UIAxes, ['X: ', num2str(xClicked), ', Y: ', num2str(yClicked)]);
% Insert additional processing logic as required
end
end
Next, within your plotting function, such as `startupFcn`, ensure to assign the plot to the lineObj property and set the ButtonDownFcn accordingly:
function startupFcn(app)
defaultX = 0:0.1:2*pi; % Example X data
defaultY = sin(defaultX); % Example Y data
% Generate the plot with the default dataset
app.lineObj = plot(app.UIAxes, defaultX, defaultY);
% Assign the callback function for mouse click events on the plot
app.lineObj.ButtonDownFcn = @app.lineObjButtonDown;
% Configure additional plot properties as desired
end
With this setup, any click on the plotted line within the UIAxes will trigger the lineObjButtonDown method, capturing the clicked coordinates. These values will be displayed both in the MATLAB command window and as a title on the plot itself for immediate reference.
I hope this helps.

その他の回答 (1 件)

KSSV
KSSV 2024 年 1 月 17 日
REad about getpts

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by