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

12 次查看(过去 30 天)
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?

采纳的回答

Adam Danz
Adam Danz 2024-5-14
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

更多回答(1 个)

Vinayak
Vinayak 2024-5-14
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 个评论
Joel Williams
Joel Williams 2024-5-14
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by