Receiving an error when using interp2 to interpolate longitude and latitude (Error using griddedInterpolant Grid arrays must have NDGRID structure.).

5 次查看(过去 30 天)
Hello, I have a 140x140 longitude matrix, a 140x140 latitude matrix, and a 140x140 matrix that corresponds to elevation at these longitudes and latitudes. I am trying to interpolate in order to find the elevation at specifc longitude and latitude (longitude:171.40 , latitude:-83.51) using interp2: interp2(lon,lat,elevation,171.40,-83.51);.
However I keep recieving this error:
Error using griddedInterpolant
Grid arrays must have NDGRID structure.
Error in interp2>makegriddedinterp (line 226)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 134)
F = makegriddedinterp(X, Y, V, method,extrap);
Error in untitled (line 14)
interp2(lon,lat,elevation,171.40,-83.51);
I dont understand why I keep receiving this error and I cannot figure out how to fix it. I have attached the spreadsheets with the longitude, latitude, and elevation data. Any help would be greatly appreciated. Thank you!
  2 个评论
Image Analyst
Image Analyst 2023-7-22
Make it easy for people to help you by not requiring them to write code to get your variables. Post the code to read in the data.
Sasha
Sasha 2023-7-23
Sorry about that. Here is the code (I've also attached the code as .m):
lon = xlsread('lon.xlsx') ;
lat = xlsread('lat.xlsx') ;
elevation = xlsread('elevation.xlsx') ;
x = interp2(lon,lat,elevation,171.40,-83.51) ;

请先登录,再进行评论。

回答(1 个)

Paul
Paul 2023-7-23
编辑:Paul 2023-7-23
lon = xlsread('lon.xlsx') ;
lat = xlsread('lat.xlsx') ;
elevation = xlsread('elevation.xlsx') ;
interp2 requires matrix inputs of sample points to be in meshgrid format. When plotted together, the sample points would bmake a nice array of points. For example
[X,Y] = meshgrid(1:5,2:2:10);
figure
plot(X,Y,'r.'),axis padded
But your data doesn't look like that
figure
plot(lon,lat,'r.')
Maybe scatteredInterpolant would be more appropriate
F = scatteredInterpolant(lon(:),lat(:),elevation(:));
%(longitude:171.40 , latitude:-83.51)
F(171.4,-83.51)
ans = 299.5649
Check the doc page and see if that result is what you'd expect based on the sample data.

类别

Help CenterFile Exchange 中查找有关 Geographic Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by