Data is in MESHGRID format, NDGRID format is required. INTERP2 help please!

30 views (last 30 days)
Hello,
I am trying to interpolate 2d data along a satellite track using the following code:
or yr = 1993%:2010; yr load(['/work/uo0122/u253082/Mat_Time/ssh_',num2str(yr),'.mat']); i = i+1 size(ssh_int,1); mask = ones(3600,1682,size(ssh_int,1));
%X = X'; Y = Y'; V = V'; F = griddedInterpolant(X,Y,V)
for day = 1:size(ssh_int,1);
mask(:,:,day) = mask50;
ma = permute(mask,[3 1 2]);
%f = ndgrid(ma);
m50m(:,:,day)=interp2(lonrep,latrep,squeeze(mask(:,:,day)),REF_lon,REF_lat,'linear');
end
end
where lonrep,latrep,REF_lon and REF_lat = 3600 x 1682 matrix and mask = 3600 x 1682 x 365.
This should in theory work as I have been told it works with other data. When I run this script it complains:
Error using griddedInterpolant
Data is in MESHGRID format, NDGRID format is required. Convert your data as follows: X = X'; Y = Y'; V = V'; F = griddedInterpolant(X,Y,V)
Error in interp2/makegriddedinterp (line 220) F = griddedInterpolant(varargin{:});
Error in interp2 (line 133) F = makegriddedinterp(X, Y, V, method);
Error in depth_criterion (line 52) m50m(:,:,day)=interp2(lonrep,latrep,squeeze(mask(:,:,day)),REF_lon,REF_lat,'linear');
I have tried converting to ndgrid and to meshgrid but didn't work and tried other things with no luck.
Can someone please help me???
thanks, Michael

Accepted Answer

dpb
dpb on 7 Jun 2014
...any ideas how I should rearrange the data?
I don't quite follow your words :) on the ordering but what you need is like the examples in meshgrid
Note that the two vectors x,y into meshgrid are each sorted (hence "monotonic") and then it builds the appropriate grid from them for you. You'll need to then select the Z data to correspond to those locations and have a full dataset for interp2

More Answers (1)

dpb
dpb on 7 Jun 2014
...
m50m(:,:,day)=interp2(lonrep,latrep,squeeze(mask(:,:,day)),REF_lon,REF_lat,'linear');
You don't show how lonrep/latrep are obtained; one presumes they're in the .mat file?
Anyway, as the doc for interp2 says...
I = interp2(X,Y,Z,XI,YI) returns matrix ZI containing elements corresponding to
the elements of XI and YI ... X and Y must be monotonic, and have the same format ("plaid") as if they were produced by meshgrid. ..."
One presumes that the generation of these wasn't performed by the use of meshgrid but perhaps were just read from some other data source. More than likely the issue is that the coordinates weren't actually ordered so that the resulting grid isn't monotonic in each direction.
You'll have to look at the structure of the dataset and rearrange it. That this dataset isn't consistent and another seems to work is indicative that there's the likely root cause--they "working" and "nonworking" datasets weren't created consistently with each other.
You can test this for the arrays by
all(all(diff(lonrep,[],2)>0))
all(all(diff(latrep)>0))
If either (or both) of these return(s) FALSE (0) there's your problem (and the crystal ball is betting on it... :) )
  1 Comment
Michael
Michael on 7 Jun 2014
Thanks for replying!
Lonrep & latrep are the repeated dimensions of the lat and lon from mask. So I first get 1682 x 1 for lat and then I repeat 3600, and vice versa for lon and eventually transposing lat. Ending up with two 3600 x 1682 matrices. So I need a dimension like 2420103 x 1 instead of 3600 x 1682 for example? I tried the test and I did indeed get (0). I am fairly new with Matlab so I am still trying to get my head around some of the wording they use... Before I tried the command blah = ndgrid(mask); but it took too much memory. Do you have any ideas how I should rearrange the data?
Many thanks! Michael

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!