Regrid data from 2x2 to 1x1 resolution
10 views (last 30 days)
Show older comments
CHARLES ADDEY
on 18 Feb 2022
Commented: CHARLES ADDEY
on 15 May 2022
I am trying to regrid this data from excel. I have long, lat, time and sst. I want to regrid from 2x2 to 1x1, i am getting the error feedback
"Subscripting a table using linear indexing (one subscript) or multidimensional indexing (three or more subscripts) is not supported. Use a row subscript and a variable subscript."
Please any idea how to navigate through this? The file is attached and the code i tried is written below. Thanks in advance.
t = readtable('C:\Users\hp\Desktop\DESKTOP DOCUMENTS\INTERPOLATION AND GRIDDING(MATLAB)\interp\example.xlsx')
lon=t(:,1); % Load Longitude from excel
lat=t(:,2); % Load Latitude from excel
date=t(:,3); % Load time from excel
sst=t(:,4); % Load sst from excel
new_lon=[120:1:180-1]; %120 degrees and 180 degrees
new_lat=[20:1:60]';
for date = 1: size(sst, 3) %loop through each date
new(:,:,date) =interp2(lon,lat,sst(:,:,date)',new_lon,new_lat);
end
2 Comments
Accepted Answer
CHARLES ADDEY
on 19 Feb 2022
Edited: CHARLES ADDEY
on 20 Feb 2022
2 Comments
Peter Perkins
on 5 May 2022
Charles, I'm late to this party, and if that code works for you, that's great. But I have a couple of observations.
xlsread is not recommended any more. readtable was the right way to go (see next). readmatrix might have worked if you specify a range that expludes the date column in your file, but that's kind of a pain to do because it's in the middle.
The problem in your original code was in getting the lat/lon out of the table. This
lon=t(:,1)
creates a table with one variable, which interp2 is not going to be happy with. What you wanted was lon = t.lon, and in fact, you don't even need to extract it:
new(:,:,date) = interp2(t.lon,t.lat,...)
Except that t.lat and t.lon, as in your file, are "columnized" versions of what are really matrices. So maybe either
interp2(reshape(t.lon,n,m),reshape(t.lat,n,m)...)
or
interp2(unique(t.lon),unique(t.lat),...)
Except that your file is 158 rows, so ... lat and lon do not have the right number of values to be n-by-m. You've used griddata, which is for scattered data. Maybe that's why. It's not clear that using griddata on data that are already on a grid is a good idea, because although it works, interp2 would use that knowledge. Maybe it's fine, I'm not sure how griddata works internally. So I don't know what your real data look like. It helps to provide something accurate.
Then there's SST. your file only has one day, while your original code loops over days. If your intent is to upsample each day separately, then you do need a loop as in your original code. Actually, this seems like a candidate for a grouped rowfun. Tables often make things easier, no exception here.
What you need first is a function to do the upsampling on one day's data. To use interp2, it would look something like:
function [latNew,lonNew,sstNew] = fun(lat,lon,sst)
[latNew,lonNew] = meshgrid(120:1:170,25:1:60);
lat = reshape(lat,29,6);
lon = reshape(lon,29,6);
sst = reshape(sst,29,6);
sstNew = interp2(lat,lon,sst,latNew,lonNew);
latNew = latNew(:);
lonNew = lonNew(:);
sstNew = sstNew(:);
but of course your sample data are the wrong length so that won't work. Using griddata, it's probably as simple as
function tNew = tmp1
t = readtable("example.xlsx");
tNew = rowfun(@fun,t,"GroupingVariable","date","OutputVariableNames",["lat" "lon" "sst"]);
function [latNew,lonNew,sstNew] = fun(lat,lon,sst)
[latNew,lonNew] = meshgrid(120:1:170,25:1:60);
latNew = latNew(:);
lonNew = lonNew(:);
sstNew = griddata(lat,lon,sst,latNew,lonNew);
except of course that returns mostly NaNs because your sample data don't span the lat/lon range it interpolates to.
More Answers (1)
Matt J
on 19 Feb 2022
Edited: Matt J
on 19 Feb 2022
Don't read in the imported data as a table. Read it in as a matrix.
2 Comments
Walter Roberson
on 19 Feb 2022
Also, after either readtable or readmatrix, there is no way that any of the variables can end up 3d. Both of these functions return column vectors for each variable, with the columns possibly being cells to hold text. (Exception: if you use specific configuration steps, a variable could end up being a 2d character array instead of a cell array of character vectors. But none of the options allow the functions to read a variable as 3d)
See Also
Categories
Find more on Tables 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!