Select points from a 3D plot by using the mouse

50 views (last 30 days)
Hi everybody,
I have plotted a Matrix Nx3 that represents the coordinates of my surface in the space by using the function plot3.
I was wondering if there is a function that allows me to select only some points of my Matrix from the plot and store these points in a new matrix

Answers (1)

Rishabh Mishra
Rishabh Mishra on 24 Jan 2021
Hi,
Based on description of your issue, I understand that you are trying to extract the coordinates of the point (on a plot) selected interactively. Therefore, I have developed a code using interactive graphics in MATLAB that serves the purpose:
handle.a = axes;
% generate random x,y,z values
handle.x = randi([1 10],1,5);
handle.y = randi([1 10],1,5);
handle.z = randi([1 10],1,5);
% plot in 3D
handle.p = plot3(handle.x,handle.y,handle.z,'.');
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
% add callback when point on plot object 'handle.p' is selected
% 'click' is the callback function being called when user clicks a point on plot
handle.p.ButtonDownFcn = {@click,handle};
% definition of click
function click(obj,eventData,handle)
% co-ordinates of the current selected point
Pt = handle.a.CurrentPoint(2,:);
% find point closest to selected point on the plot
for k = 1:5
arr = [handle.x(k) handle.y(k) handle.z(k);Pt];
distArr(k) = pdist(arr,'euclidean');
end
[~,idx] = min(distArr);
% display the selected point on plot
disp([handle.x(idx) handle.y(idx) handle.z(idx)]);
end
Please note that in the last line of the code, we are simply displaying the coordinates of points that are selected, one after the another. Instead of displaying the coordinates of the points, you can also store them in a matrix.
Hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by