Effacer les filtres
Effacer les filtres

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

44 vues (au cours des 30 derniers jours)
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 commentaire
Mann Baidi
Mann Baidi le 17 Jan 2024
"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?

Connectez-vous pour commenter.

Réponse acceptée

Avni Agrawal
Avni Agrawal le 17 Jan 2024
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.

Plus de réponses (1)

KSSV
KSSV le 17 Jan 2024
REad about getpts

Catégories

En savoir plus sur Specifying Target for Graphics Output dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by