How to get values of X,Y,Z when i point courser on the graph
62 views (last 30 days)
Show older comments
Ruthvik sainath meka
on 23 Jul 2019
Here is the graph what i have
how to get the values of X,Y,Z when i point the courser on the graph?
i need a code which gives me information of all the paramets on the graph at that point
4 Comments
Adam Danz
on 23 Jul 2019
I agree with Adam that these data would work well with an imaged. What function did you try and what was the error?
Accepted Answer
Adam Danz
on 23 Jul 2019
Edited: Adam Danz
on 23 Jul 2019
Method 1: use data tips
% Create demo scatter plot
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
sz = 25;
c = linspace(1,10,length(x));
s = scatter(x,y,sz,c,'filled');
% Add an additional row to the data tips showing the color value
datatipRow = dataTipTextRow('C',c);
s.DataTipTemplate.DataTipRows(end+1) = datatipRow;
% Now click on any point to produce the data tip (image below)
Method 2: return value to command window (or anywhere else you'd like)
This uses ginput() which allows the user to click on any space within the axes. It then computes the nearest point and returns its (x,y) coordinate and color value.
% User must select 1 point
[x,y] = ginput(1);
% Get (x,y) coordinates for all points
% *** This assumes the entire point was made with the one
% call to scatter() instead of several calls to scatter().
h = gco();
hx = h.XData;
hy = h.YData;
hc = h.CData;
% Find the nearest point to selection
d = sqrt((x-hx).^2 + (y-hy).^2);
[~,minIdx] = min(d);
% Return the (x,y) and color data
fprintf('(x,y,c) = (%.3f,%.3f,%.3f)\n',hx(minIdx),hy(minIdx),hc(minIdx));
2 Comments
Adam Danz
on 23 Jul 2019
Edited: Adam Danz
on 25 Jul 2019
"can there be any modification..."
Yes. One way to do it is by increasing the number in ginput(). For exampe, ginput(10) requires that the user select 10 points before anything happens. The [x,y] output will be vectors of 10. You'll also need to modify the fprintf() command unless you've implemented a different, more useful output.
Another way is to put method #2 in a loop. Let me know if you get stuck.
More Answers (0)
See Also
Categories
Find more on Discrete Data Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!