Input grid is not a valid MESHGRID
54 views (last 30 days)
Show older comments
I am trying to interpolate values with interp2 but it gives the error: input grid is not a valid meshgrid. I've used interp2 before but the only change i made this time was transpose the X,Y matrices so that it matches with the output matrix I have.
Particularly, I need help wrapping my head around X and Y coordinates for the interp2 since it does not seem to work. I had initially transposed the X and Y meshgrids because my raw data was a 62x63 matrix with 62 x position and 63 y positions, but the meshgrids were 63x62. I was able to make a quiver plot successfully doing this.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/667360/image.jpeg)
Now, I need to interpolate this raw data across a finer resolution/sampling with interpolation with interp2. However, I am confused why I cannot do this easily with interp2 but for some reason it works when i switch my X and Y transposed meshgrids (see variables B and C). However the resulting quiver plot looks inaccurate and just wrong.
A= loadvec('2009-08-07_measurements_22mi_e16900.T001.D001.P001.H001.L.vec');
[X,Y] = meshgrid(A.x,A.y);
Xprime = X';
Yprime = Y';
quiver(Xprime,Yprime,A.vx,A.vy)
xq = linspace(0,23,500);
yq = linspace(0,23,500);
[Xq,Yq] = meshgrid(xq,yq);
velocitydata = A.vx;
velocitydata2= A.vy;
B = interp2(Yprime,Xprime,velocitydata,Xq,Yq, 'spline');
C = interp2(Yprime,Xprime,velocitydata2,Xq,Yq,'spline');
figure(2);
quiver(Xq,Yq,B,C)
5 Comments
Stephen23
on 28 Jun 2021
"...but the only change i made this time was transpose the X,Y matrices..."
Which exactly why you are getting that error message: interpolation routines require data to be in particular way, and by transposing those matrices you have arranged the data differently.
If you want to use INTERP2, then consider swapping the order of the inputs and outputs:
[Y,X] = meshgrid(A.y,A.x);
Accepted Answer
KSSV
on 28 Jun 2021
A= loadvec('2009-08-07_measurements_22mi_e16900.T001.D001.P001.H001.L.vec');
[X,Y] = meshgrid(A.x,A.y);
Xprime = X ;
Yprime = Y ;
quiver(Xprime,Yprime,A.vx',A.vy')
xq = linspace(min(A.x),max(A.x),500);
yq = linspace(min(A.y),max(A.y),500);
[Xq,Yq] = meshgrid(xq,yq);
velocitydata = A.vx;
velocitydata2= A.vy;
B = interp2(Xprime,Yprime,velocitydata',Xq,Yq, 'spline')';
C = interp2(Xprime,Yprime,velocitydata2',Xq,Yq,'spline')';
figure(2);
quiver(Xq,Yq,B',C')
2 Comments
More Answers (0)
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!