Index in position 2 exceeds array bounds (must not exceed 101)

I would appreciate any help on this. Problem is defining 'newsst', i have tried with also with a loop (commented) but theres something dimension wise i dont get..
Have attached the data and hope the code is descriptive enough.
% load and store netcdf file variables in separate matrices
ncdisp('sst_2015.nc')
sst = ncread('sst_2015.nc','sea_surface_temperature');
time = ncread('sst_2015.nc','time');
lat = ncread('sst_2015.nc','lat'); %latitude
lon = ncread('sst_2015.nc','lon'); %longitude
%exclude potential values from outside the defined box of coordinates below
% Note: use of '&&' instead of '&' below creates an error: 'Operands to the || and &&
% operators must be convertible to logical scalar values'. Works with '&',
% not sure if correct.
newlat = find(lat>50&lat<60);
newlon = find(lon>-3&lon<11);
%change to desired degree of resolution for latitude and longitude
ds_lat=0.1000;
ds_lon = 0.1666;
lati=50:ds_lat:60;
loni=-3:ds_lon:11;
[Loni,Lati] = meshgrid(loni,lati) ;
newsst = sst(newlat,newlon,time);
% newsst = zeros (i,j,k); i,j,k loop base on dimensions of sst
% for i=1:140
% for j=1:101
% for k=1:355
%
% newsst = sst(newlat(i),newlon(j),time(k));
%
% end
%
% end
% end
%interpolate and put everything in a new grid. we will now have values on same
%lat and lon points defined by Loni and Lati
NEWSST = griddata (Loni,Lati,newlat,newlon,newsst);

 Respuesta aceptada

Walter Roberson
Walter Roberson el 18 de Mzo. de 2019
netcdf files return arrays with rows and columns exchange relative to what MATLAB uses. You need to index sst(newlon, newlat, time_index)
Do not use the time as an index: it is in POSIX time format, seconds since 1970, values over 1 billion, covering Jan 1, 2004 00:00 to Dec 30, 2004 00:00 .

5 comentarios

thanks, have changed it to
newsst = sst(newlon,newlat,:);
and it works, but then the
NEWSST = griddata (Loni,Lati,newlon,newlat,newsst);
line is bugging with 'invalid input arguments' (error(message('MATLAB:griddata:InvalidInputArgs'));)
NEWSST = griddata(Loni, Lati, newsst, newlon, newlat)
tried it, still bugging:
Error using griddata (line 87)
Invalid input arguments.
Error in sst_test (line 31)
NEWSST = griddata(Loni, Lati, newsst, newlon, newlat);
Code attached.
The interpolation is a bit slow.
Note that the output will be 101 x 85 x 335 .
Note that interpolation for any one time might be affected by earlier and later times. This is a 3D interpolation, not one independent 2D interpolation for each time.
Thank you, works perfectly!

Iniciar sesión para comentar.

Más respuestas (1)

My guess is that you want all the times, in which case:
newsst = sst(newlat, newlon, :);
Using time as an index into newsst makes no sense.
Note that you don't need find, it's just a waste of time. You can use the logical array directly to index sst:
newlat = lat>50 & lat<60; %find not required. Just extra processing for no reason
newlon = lon>-3 & lon<11; %same
newsst = sst(newlat, newlon, :);

2 comentarios

Well i have followed your advice and now
newsst = sst(newlat,newlon,:);
returns with error message 'The logical indices in position 2 contain a true value outside of the array bounds'
have changed it to
newsst = sst(newlon,newlat,:);
instead and it works, but then the
NEWSST = griddata (Loni,Lati,newlon,newlat,newsst);
line is bugging with 'invalid input arguments' (error(message('MATLAB:griddata:InvalidInputArgs'));)

Iniciar sesión para comentar.

Categorías

Preguntada:

el 18 de Mzo. de 2019

Comentada:

el 18 de Mzo. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by