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

136 次查看(过去 30 天)
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!

采纳的回答

Adam Danz
Adam Danz 2019-8-12
编辑:Adam Danz 2019-8-16
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.
  29 个评论
Diptangshu Paul
Diptangshu Paul 2024-10-17
Dear Altman, this is a very simple code, yet a beautiful solution to a big problem. However for my purpose, I need to store these [coordinateSelected, minIdx] into two variables. How can it be done?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by