![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1482376/image.png)
Interpolation of a point from contourf
8 views (last 30 days)
Show older comments
I need to interpolate the x,y coordinates for Z=1 in the following contour:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1084580/image.png)
the point 1 doesn't exist in my data.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1084585/image.png)
and my data:
x:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1084590/image.png)
y:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1084595/image.png)
I have figured the existing point closest to one, but I need 1 exactly.
Thank you very much!
0 Comments
Answers (1)
Saarthak Gupta
on 14 Sep 2023
Hi,
I understand you are trying to find the X and Y values for the level Z = 1. According to the query, the z-coordinates you have given as input to the ‘contourf’ function do not contain 1.
You can use a list of levels where the contour lines will be displayed and include 1 in it.
Refer to the following code:
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X) + cos(Y);
% Specify 10 levels, and explicitly include 1 in the list of levels
levels = [linspace(-pi/2,1,4) 1 linspace(1,pi/2,5)];
M = contourf(X,Y,Z,levels);
% To check if 1 is part of the levels, get the list of levels from the contour matrix
levelList = findAllLevels(M);
function levels = findAllLevels(M)
% This helper function extracts all levels/heights from the contour matrix returned by contourf
n=size(M,2);
j=1;
i=1;
while i+M(2,i)+1<n
i = i+M(2,i)+1;
levels(j) = M(1,i);
j = j+1;
end
end
This would ensure a contour line is plotted at Z = 1, and the x and y coordinates at this contour line can be obtained through the contour matrix ‘M’. You can observe the same by inspecting ‘M’ in the editor window:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1482376/image.png)
Please refer to the following MATLAB documentation for further reference:
0 Comments
See Also
Categories
Find more on Contour Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!