Active "data tips" and then you can click on any plotted coordinate to return the (x,y,z) values. From r2018b to present, the toolbar becomes visible when you hover over the axes. Prior to r2018b, the toolbar that contains the data tip icon is at the top of the figure. More info on that (link). " ...or even better can be extracted for other calculations"
To return the coordinate selected by a mouse click, you can assign a ButtonDownFcn to the plotted object handle. Within the ButtonDownFcn you can determine which of your coordinates were closest to your mouse-click and then return that coordinate. Here's a complete demo that returns the entire (x,y,z) coordinate you selected. If you just want z, run this function and then extract z from the first output.
clf()
axh = axes();
x = rand(1,20);
y = rand(1,20);
z = rand(1,20);
h = plot3(axh, x, y, z, 'ko');
xlabel('x axis')
ylabel('y axis')
zlabel('z axis')
grid on
h.ButtonDownFcn = @showZValueFcn;
function [coordinateSelected, minIdx] = showZValueFcn(hObj, event)
x = hObj.XData;
y = hObj.YData;
z = hObj.ZData;
pt = event.IntersectionPoint;
coordinates = [x(:),y(:),z(:)];
dist = pdist2(pt,coordinates);
[~, minIdx] = min(dist);
coordinateSelected = coordinates(minIdx,:);
fprintf('[x,y,z] = [%.5f, %.5f, %.5f]\n', coordinateSelected)
end
*An older version of this answer assigned the ButtonDown function to the axes instead of the line object.
Best Answer