MATLAB: Specifying 3D points with the mouse

ginput plot3d

I would like to be able to specify points on the X-Y plane with the mouse, but do so on a 3D plot. For any give point clicked, there should be a unique position on the X-Y plane (except in the degenerate case when the camera is on the X-Y plane).
The math is fairly simple if I can get the figure coordinates clicked by the mouse on a 3D plot. If I recover the projection matrix with T = view;, I think I should be able to back out the transformation by T if I hold z=0.
However, ginput is not returning sensible values when I try this. I am not able to determine what it is trying to return but it is not useful.
Any suggestions?

Best Answer

In short, don't use ginput(), use get(ax,'CurrentPoint') instead.
Basically the current position of the mouse, combined with the view matrix defines a stabbing vector. It will be up to you to interpret which plane that stabbing vector should intersect. Here is an example which prints out a coordinate in the view axes whenever the mouse moves. You could instead assign it to a 'onButtonPress' callback if you like. I'll be honest and say I can't quite remember which plane the view matrix uses by default, but it sounds from your question like you're on top of interpreting the result of the matrix transformation and are more interested in the replacement of ginput() with something more useful:
function example
figure('WindowButtonMotionFcn',@(src,evnt)printPos())
plot3(rand(5,1),rand(5,1)*20,rand(5,1)*5,'.'), view(3)
function printPos
clickedPt = get(gca,'CurrentPoint');
VMtx = view(gca);
point2d = VMtx * [clickedPt(1,:) 1]';
disp(point2d(1:3)')
end
end