why interp2 report "input grid is not a valid meshgrid" since input grid is generated by meshgrid?

Dear Community,
I need to do a 2D interpolation for a matrix data, but when I use the interp2, the function report error "input grid is not a valid meshgrid". I am really confused since the input grid is generated by meshgrid function. If anyone knows this issue, could you please give me some suggestions?
% original coordinate
je=167;
ke=117;
dy=4e-8;
dz=dy;
ya=[0:dy:je*dy];
za=[0:dz:ke*dz];
ya=ya-mean(ya);
za=za-mean(za);
% interpolation
load data.mat
[Z,Y]=meshgrid(za,ya);
aymap = interp2(Y,Z,ay,squeeze(newcord_Y),squeeze(newcord_Z));

 Accepted Answer

load("data.mat") ;
% original coordinate
je=167;
ke=117;
dy=4e-8;
dz=dy;
ya=[0:dy:je*dy];
za=[0:dz:ke*dz];
ya=ya-mean(ya);
za=za-mean(za);
% interpolation
load data.mat
[Zi,Yi]=meshgrid(za,ya);
Y = squeeze(newcord_Y) ;
Z = squeeze(newcord_Z) ;
aymap = interp2(Y,Z,ay,Yi,Zi);

6 Comments

I think you misunderstand what I mean. The value of ay corresponds to the old coordinate [Z,Y]=meshgrid(za,ya). But even though I use your script directly , the MATLAB R2015a still reports same error "Error using interp2>makegriddedinterp (line 237), Input grid is not a valid MESHGRID". Thanks for your attention. Could you please give more suggestions?
Try griddata
load("Data.mat") ;
ke=117-1;
je=167-1;
dy=4e-8;
dz=dy;
ya=[0:dy:je*dy];
za=[0:dz:ke*dz];
ya=ya-mean(ya);
za=za-mean(za);
% interpolation
load data.mat
[Z,Y]=meshgrid(za,ya);
Yi = squeeze(newcord_Y) ;
Zi = squeeze(newcord_Z) ;
aymap = griddata(Y,Z,ay,Yi,Zi);
Thanks a lot. The "griddata" works well now.
Strangely, why the "interp2" function has issue since the grid is generated by MESHGRID?
interp2 is working for me. Thanks is accepting the answer. :)
by the way, do you have any idea why "interp2" doesn't work for me? does it come from version bugs?

Sign in to comment.

More Answers (1)

"If anyone knows this issue..."
Explanation: you swapped around the order of the variables:
[Z,Y] = meshgrid(za,ya); % order: Z,Y
interp2(Y,Z,...) % order: Y,Z
By design meshgrid swaps the first dimensions anyway to make them compatible with X-Y coordinates that people often prefer to use for plotting. The documentation explains how the data is arranged in the output arrays, I will not reproduce it here, but suffice to say by swapping the array order the data is arranged incorrectly for interp2.
Note that the correct array order is clearly shown in all of the interp2 examples, e.g.:
[Xq,Yq] = meshgrid(-3:0.25:3); % order: Xq,Yq
Vq = interp2(X,Y,V,Xq,Yq); % order: Xq,Yq
Solution: use the variables in the same order.
It is worth nothing that the first line of the interpolation functions state:
  • for interp2 use meshgrid
  • for interpn use ndgrid
and then you simply need to provide the arguments in the same order (unlike your code).
See also a more detailed explanation here:

1 Comment

Highly appreciate the root reasons you digged out. I tried your way and now "interp2" works well too.

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Products

Release

R2015a

Asked:

on 5 Jun 2020

Commented:

on 5 Jun 2020

Community Treasure Hunt

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

Start Hunting!