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

175 ビュー (過去 30 日間)
Leon
Leon 2019 年 8 月 12 日
コメント済み: Adam Danz 2019 年 8 月 16 日
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.
Adam Danz
Adam Danz 2019 年 8 月 16 日
Glad I could help out!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by