x = contourf(:,1);

y = contourf(:,2);

z = contourf(:,3);

[X,Y] = meshgrid(x,y) ;

zg = griddata(x,y,z,X,Y) ;

[Cx, h] = contourf(X,Y,zg,15);

2 views (last 30 days)

Show older comments

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.

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.

surf(X,Y,Z,'EdgeColor','none')

hold on

contour3(X,Y,Z,15,'EdgeColor','k')

% Overlay the original data points

plot3(x,y,z,'k.');

% Overlay the query data points.

plot3(xq,yq,zq,'ro');

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

Start Hunting!