How do I programmatically extract a 3D plot point selected by a user interactively?
8 views (last 30 days)
Show older comments
Joel Williams
on 14 May 2024
Commented: Joel Williams
on 14 May 2024
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?
0 Comments
Accepted Answer
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
0 Comments
More Answers (1)
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'.
You can find more details on this in this MATLAB answer: https://in.mathworks.com/matlabcentral/answers/1736325-how-can-i-select-a-whole-row-from-a-uitable-when-clicking-on-a-cell-in-that-row.
Hope this helps you out!
See Also
Categories
Find more on Develop uifigure-Based Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!