How to enable a figure so that if I click on a point and it will show the value?

213 views (last 30 days)
If I have a three column data of x, y, and z, how do I make a plot of x vs y, so that if I click on a point, the value z will show up on the figure next to the point, or even better can be extracted for other calculations.
Thanks!

Accepted Answer

Adam Danz
Adam Danz on 12 Aug 2019
Edited: Adam Danz on 16 Aug 2019
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.
% Run this independently. A random 3D array of dots will be drawn. Click on
% any marker to invoke the showZValueFcn function. See comments for more detail
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')
% view(0,90) % to view as 2D
grid on
h.ButtonDownFcn = @showZValueFcn;
% axh.ButtonDownFcn = {@showZValueFcn, x, y, z}; % old version of answer
function [coordinateSelected, minIdx] = showZValueFcn(hObj, event)
% FIND NEAREST (X,Y,Z) COORDINATE TO MOUSE CLICK
% Inputs:
% hObj (unused) the axes
% event: info about mouse click
% OUTPUT
% coordinateSelected: the (x,y,z) coordinate you selected
% minIDx: The index of your inputs that match coordinateSelected.
x = hObj.XData;
y = hObj.YData;
z = hObj.ZData;
pt = event.IntersectionPoint; % The (x0,y0,z0) coordinate you just selected
coordinates = [x(:),y(:),z(:)]; % matrix of your input coordinates
dist = pdist2(pt,coordinates); %distance between your selection and all points
[~, minIdx] = min(dist); % index of minimum distance to points
coordinateSelected = coordinates(minIdx,:); %the selected coordinate
% from here you can do anything you want with the output. This demo
% just displays it in the command window.
fprintf('[x,y,z] = [%.5f, %.5f, %.5f]\n', coordinateSelected)
end % <--- optional if this is embedded into a function
*An older version of this answer assigned the ButtonDown function to the axes instead of the line object.
Alternatives: see "Method 2" in this answer.
  28 Comments
Leon
Leon on 16 Aug 2019
OK! That concludes my questions! I really appreciate your tremendous help very much!
I'm sure the program you helped me create will benefit many future users as well.

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!