Regrid data from 2x2 to 1x1 resolution
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
CHARLES ADDEY
am 18 Feb. 2022
Kommentiert: CHARLES ADDEY
am 15 Mai 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 Kommentare
Akzeptierte Antwort
CHARLES ADDEY
am 19 Feb. 2022
Bearbeitet: CHARLES ADDEY
am 20 Feb. 2022
2 Kommentare
Peter Perkins
am 5 Mai 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.
Weitere Antworten (1)
Matt J
am 19 Feb. 2022
Bearbeitet: Matt J
am 19 Feb. 2022
Don't read in the imported data as a table. Read it in as a matrix.
2 Kommentare
Walter Roberson
am 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)
Siehe auch
Kategorien
Mehr zu Tables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!