Finding surface height at x,y coordinates

14 ビュー (過去 30 日間)
LMS
LMS 2016 年 4 月 19 日
コメント済み: LMS 2016 年 4 月 19 日
Dear all,
using the mapping toolbox, I've created an interpolated surface from a scattered dataset. x,y,z are read from a file.
[yi,xi] = meshgrid(min(y):0.5:max(y), min(x):0.5:max(x)); % sets up mesh grid for x/y coords, step-size=0.5
zi = griddata(y,x,z,yi,xi,'linear'); % interpolates z values
surfm(yi,xi,zi);
Now, I need to read out the interpolated zi value at a given location (say... -10, -50). I feel like there's a very simple solution, but half an hour of googling and reading up in the documentation hasn't led me to success. Could someone point me in the right direction? Thank you!
  1 件のコメント
Stephen23
Stephen23 2016 年 4 月 19 日
What do you mean by "I need to read out the interpolated zi value": do you want to click on the figure and get the z value, or do you want to calculate the z value by passing the x,y location values?

サインインしてコメントする。

採用された回答

Teja Muppirala
Teja Muppirala 2016 年 4 月 19 日
Instead of GRIDDATA, I think "scatteredInterpolant" is what you want. This creates an object that you can use to query points freely.
% Make some data
x = -30+60*rand(1000,1);
y = -30+60*rand(1000,1);
z = sin(x/10).*sin(y/15);
% set up the scatteredInterpolant object
method = 'linear';
extrap = 'none';
S = scatteredInterpolant(x,y,z,method,extrap)
% Evaluate on the grid
[yi,xi] = meshgrid(min(y):0.5:max(y), min(x):0.5:max(x)); % sets up mesh grid for x/y coords, step-size=0.5
zi = S(xi,yi); % <-- This is equivalent to zi = GRIDDATA(...)
% You can call S(x,y) to evaluate at specific points
S(-10,20)
  1 件のコメント
LMS
LMS 2016 年 4 月 19 日
Thank you SO MUCH! This works perfectly.

サインインしてコメントする。

その他の回答 (1 件)

John D'Errico
John D'Errico 2016 年 4 月 19 日
You have already used griddata! It looks like you have found a code snippet, but have no clue as to what it does or why it works. So read the help for griddata.
Now you wish to interpolate at some general point. You can either use griddata again at that general point, or since you have apparently now formed a regular grid you COULD use interp2.
The latter seems logical, but it is in fact worse, because then you have two sources of interpolation error. So just use griddata, as you did already.
  1 件のコメント
LMS
LMS 2016 年 4 月 19 日
JFC John, you're killing me. This is my code and I absolutely do understand what it does. I've been through the documentation of "griddata", but it doesn't mention how to query for coordinates. I tried every phrasing I could come up with but I seem to be missing something. Would it kill you to get off your high horse and just give me the correct way to code this?

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by