bug with interp2 ?
8 views (last 30 days)
Show older comments
Hello,
I want to interpolate a 2D data "v" from a 2km latitude and longitude grid (xx and yy) to a 10 km grid (Xq and Yq). but with interp2 I get the following error :
>> [Xq,Yq] = ndgrid(lon_extraction,lat_extraction); %
>> whos xx yy v X Y
Name Size Bytes Class Attributes
X 23x83 15272 double
Y 23x83 15272 double
v 407x793 2582008 double
xx 407x793 2582008 double
yy 407x793 2582008 double
>> h_extr_interp = interp2(xx,yy, v, X,Y);
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);
>>
(I get the same result using meshgrid instead of ndgrid)
any ideas why I get this message??
3 Comments
Stephen23
on 3 May 2023
Edited: Stephen23
on 3 May 2023
"(I get the same result using meshgrid instead of ndgrid)"
Your code shows that you do not use the outputs of NDGRID, so changing the function to MESHGRID will not change that.
"Problem solved using griddata instead of interp2"
If you have data in ND-grid format, then use INTERPN:
The MATLAB documentation explains how to select the operator based on the data arrangement:
Matt J
on 3 May 2023
Problem solved using griddata instead of interp2
griddata is quite a bit slower than interp2. You should not use it unless you are certain your xx,yy locations don't define a regular lattice.
Answers (1)
Walter Roberson
on 2 May 2023
Moved: Walter Roberson
on 2 May 2023
[Xq,Yq] = ndgrid(lon_extraction,lat_extraction);
h_extr_interp = interp2(xx,yy, v, X,Y);
What do xx and yy or X and Y have to do with Xq and Yq ? You build an ndgrid but you do not use it in the interp2
5 Comments
Stephen23
on 3 May 2023
Edited: Stephen23
on 3 May 2023
Your data are scattered, they are not on a grid:
S = load('test_bug.mat')
S.xx
S.yy
The rows and columns clearly do not consist of identical values. Therefore your data are not gridded.
Compare against the perfectly gridded sample points returned by NDGRID or MESHGRID:
[X,Y] = meshgrid(0:3,4:7)
So you will need to use a scattered interpolant (not a gridded interpolant) e.g. GRIDDATA:
See Also
Categories
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!