How do I programmatically extract a 3D plot point selected by a user interactively?
显示 更早的评论
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?
采纳的回答
更多回答(1 个)
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'.
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!
类别
在 帮助中心 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!