I have a contourf function constituted by x, y and z vectors (It is a large matrix). Then, I would like to find the x, y and z values corresponding to certain x and y vectors.

2 views (last 30 days)
I was plotting something with contourf. I created a beautiful picture with many x, y and z vectors. Then, I have certain x and y values which I would like to know their z value corresponding to the contourf.
For instance, below you can find the values used to create the contourf graph. However, I am not able to obtain the x ,y z related to the x and y points that I also attached.
Could you kindly help me please? I would be really grateful.

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 23 Nov 2021
I'm not sure I completely understand your question, but I think you are misusing meshgrid in your code example above.
Does this do what you want?
% Read the `contourf.txt` and `x and y points that I am interested in.txt` points into MATLAB
data = readmatrix('contourf.txt');
query = readmatrix('x and y points that I am interested in.txt');
x = data(:,1);
y = data(:,2);
z = data(:,3);
xq = query(:,1);
yq = query(:,2);
% Use griddate to determine the value at each of the query x/y points.
[Xq,Yq, zq] = griddata(x,y,z,xq,yq);
% If I understand correctly, the value of zq is what you are looking for.
% The rest of this code is just to visualize the results.
% Create X/Y grids based on the range of the input data.
n = 50;
[X,Y] = meshgrid(linspace(min(x),max(x),n), linspace(min(y),max(y),n));
% Sample the X/Y grid using griddate.
Z = griddata(x,y,z,X,Y);
% Draw the contour lines overlayed on a surface plot.
hold on
% Overlay the original data points
% Overlay the query data points.

More Answers (0)


Find more on Interpolation 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!