Clear Filters
Clear Filters

How do I programmatically extract a 3D plot point selected by a user interactively?

11 views (last 30 days)
I have a uifigure which includes a uitable and a plot axes. Some of the data columns in the uitable are plotted as scatter3 objects in the plot. When a user clicks a point in the plot associated with one of the scatter3 objects, I want to programmatically select the corresponding row in the uitable used to generate the point. I'm therefore wanting to execute a function when the user clicks the uifigure (presumably through the ButtonDownFcn callback) that extracts the 3D point nearest the user click position so I can determine which row in the uitable represents the selected point. This functionality is close to the Data Tip capability available for plots, but I don't want to show the data tip; I just want to get the 3D point coordinates. How can I get the 3D coordinates of the point nearest the user click position?

Accepted Answer

Adam Danz
Adam Danz on 14 May 2024
Finding the nearest point in 3D space to a mouse click in a 2D representation may pose problems because depth of the mouse click in the 3D volume is ambiguous. Instead, consider assigning a ButtonDownFcn to the Scatter object that returns its coordinates and whatever other information you'd like to return.
Here's an implementation of that idea using plot3 instead of scatter3 but the rest is the same.
Similar example using scatter

More Answers (1)

Vinayak
Vinayak on 14 May 2024
Hi Joel,
To find the nearest point to where a user clicks, you can calculate the distance from the click to all points. It's a great workaround since DataTips require precision and might not always capture the closest point based on a user's click.
Here's how you can set it up using the "ButtonDownFcn" callback on the "UIAxes":
function UIAxesButtonDown(app, event)
clickPos = event.IntersectionPoint;
scatterX = app.scatter3Data(:, 1);
scatterY = app.scatter3Data(:, 2);
scatterZ = app.scatter3Data(:, 3);
distances = sqrt((scatterX - clickPos(1)).^2 + (scatterY - clickPos(2)).^2 + (scatterZ - clickPos(3)).^2);
[~, minIndex] = min(distances);
app.UITable.Selection = minIndex;
end
This code snippet will select the row in your table that corresponds to the nearest point, just like you wanted. Remember, if you're looking to select rows by a single index, your "UITable" should have its "SelectionType" set to 'row'.
Hope this helps you out!
  1 Comment
Joel Williams
Joel Williams on 14 May 2024
This approach certainly worked, but Adam Danz's suggestion to use a callback function associated with the scatter plots themselves gave me the needed precision when selecting points.

Sign in to comment.

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!