Find z value out of x and y values from a countour in command window

11 Ansichten (letzte 30 Tage)
agung pratama
agung pratama am 5 Okt. 2020
Bearbeitet: Star Strider am 5 Okt. 2020
Hello guys i have data of x,y,and z value of a countour.
How to make z value appear by knowing x and y value in command window? Instead of clicking b in workspace.
  2 Kommentare
Adam Danz
Adam Danz am 5 Okt. 2020
How do you have the x,y,z data organized (I can't tell from the image you shared)?
If you know x and y, all you need to do is find their indices to and use them to look up z, assuming the data are organzed efficiently.
agung pratama
agung pratama am 5 Okt. 2020
I'm using the surf plot, I clicked b in workspace to see the x,y,z values. I forgot how we can check z value by comment in command window

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Star Strider
Star Strider am 5 Okt. 2020
Clicking on the contours will only show you the level of the contour. To see the value labels of the contours use ShowText:
contour(X,Y,Z,'ShowText','on')
.
  4 Kommentare
agung pratama
agung pratama am 5 Okt. 2020
I'm using surf plot. I can see z value after clicking b in workspace and search z value that I want. But Ijust knew that we can also find the z value bg type a comment in command window, somehow I forget the comment. I already trying to remember like you can see in the picture.
Star Strider
Star Strider am 5 Okt. 2020
Bearbeitet: Star Strider am 5 Okt. 2020
My idea was to do exactly that, and display the results in a text object on the surf plot.
You would need to write the code to get the data from your matrix (or interpolate it to specific coordinates, depending on what you want to do). Using inputdlg was the way I suggested to choose the coordinates.
EDIT — (5 Oct 2020 at 20:20)
A relatively easy way of finding and displaying the ‘Z’ values for any set of (x,y) coordinates is to use the griddedinterpolant function. Since your data appear to exist as a matrix, this would likely be be straightforward to implement.
Using an example from the documentation and my own additions to it:
[x,y] = ndgrid(-5:0.8:5);
z = sin(x.^2 + y.^2) ./ (x.^2 + y.^2);
F = griddedInterpolant(x,y,z);
xq = 2; % Input From ‘inputdlg’
yq = -3; % Input From ‘inputdlg’
vq = F(xq,yq);
figure
surf(x,y,z)
hold on
plot3(xq, yq, vq, '^r', 'MarkerFaceColor','r')
hold off
text(xq, yq, vq, sprintf('(%.2f, %.2f, %.3f)\n\\downarrow', xq, yq, vq), 'HorizontalAlignment','center', 'VerticalAlignment','bottom', 'Color','r')
producing —
.

Melden Sie sich an, um zu kommentieren.


Adam Danz
Adam Danz am 5 Okt. 2020
Bearbeitet: Adam Danz am 5 Okt. 2020
A surf plot is not a contour plot. That was a little confusing. Thanks for clearing that up.
Use the handle to your surface object and to get the XData, YData, and ZData properties.
[x,y,z] = sphere(20);
h = surf(x,y,z,'FaceColor','interp');
x = h.XData;
y = h.YData;
z = h.ZData;
If you didn't save the object handle you can get it from the current axis using,
h = findobj(gca, 'type', 'surface');
Once you have the x,y,z data you can locate the nearest (x,y) values and use those indices to get the corresponding z values. Due to floating point roundoff error, unless your x and y values have very low precision, you'll need to find the nearest values to your target values. There may be more than 1 z-value corresponding to the (x,y) indices. Here's an example continued from above,
% Plot surf object
[x,y,z] = sphere(20);
h = surf(x,y,z,'FaceColor','interp','FaceAlpha',0.2,'EdgeAlpha',.3);
x = h.XData;
y = h.YData;
z = h.ZData;
axis equal
hold on
view([-25.8, 11.7])
% Identify the (x,y) targets
target = [-0.25, -0.18164]; % [x,y]
% Define a limit of precision
closeEnough = 0.001;
% Find the corresponding z values
dist = pdist2([x(:),y(:)], target);
idx = dist <= closeEnough;
% Plot results
plot3(x(idx),y(idx),z(idx), 'm*', 'MarkerSize', 10, 'LineWidth', 2)
As you can see, given a single (x,y) coordinate, there are two (or more) z coordinates.

Kategorien

Mehr zu Contour Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by