Find z value out of x and y values from a countour in command window
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/371671/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/371671/image.png)
2 comentarios
Adam Danz
el 5 de Oct. de 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.
Respuestas (2)
Star Strider
el 5 de Oct. de 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 comentarios
Star Strider
el 5 de Oct. de 2020
Editada: Star Strider
el 5 de Oct. de 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 —
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/371771/image.png)
.
Adam Danz
el 5 de Oct. de 2020
Editada: Adam Danz
el 5 de Oct. de 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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/371746/image.png)
0 comentarios
Ver también
Categorías
Más información sobre Surface and Mesh Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!