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

조회 수: 61 (최근 30일)
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 CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by