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

조회 수: 199 (최근 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.
  댓글 수: 28
Leon
Leon 2019년 8월 16일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by