How do I programmatically extract a 3D plot point selected by a user interactively?
조회 수: 11 (최근 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?
댓글 수: 0
채택된 답변
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
댓글 수: 0
추가 답변 (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!
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!