The logical indices in position 1 contain a true value outside of the array bounds.

Hi I want to read and extract the nc file variables within the boundary of my study area which is Latitude (0-14 N) and Longitude (95-126 E). I create te coding, but unable to extract the data within the boundary. I think there is problem is the id = [(Lat>=0&Lat<=14), (Lon>=95&Lon<=126)]; as in the coding. I also attach one of the example file.
clear all
clc
ncfile = 'SM_OPER_MIR_OSUDP2_20230824T210217_20230824T215537_700_001_1.nc' ; % nc file name
% To get information about the nc file
ncinfo(ncfile)
% % to display nc file
ncdisp(ncfile)
% % to read a vriable 'var' exisiting in nc file
SST = ncread(ncfile, 'SST');
SSSanomaly = ncread(ncfile, 'SSS_anom')
SSSuncorrected =ncread(ncfile, 'SSS_uncorr')
SSS = ncread(ncfile,'SSS_corr') ;
Lat = ncread(ncfile, 'Latitude');
Lon = ncread(ncfile, 'Longitude');
% SSS1 = SSS(1:end);
% Lat1 = Lat (1:end);
% Lon1 = Lon (1:end);
Data = [Lat(:),Lon(:),SSS(:)] %SSSuncorrected(:),SSSanomaly(:),SST(:)];
id = [(Lat>=0&Lat<=14), (Lon>=95&Lon<=126)];
Data1 = Data(id,:,:);
save('try.asc','Data1','-ASCII');
%here is the error as in the command window
The logical indices in position 1 contain a true value outside of the array bounds.
Error in SMOS_nc (line 31)
Data1 = Data(id,:,:);

1 Comment

Lat = ncread(ncfile, 'Latitude');
Lon = ncread(ncfile, 'Longitude');
Are those read in as 2D arrays or as vectors?

Sign in to comment.

 Accepted Answer

id = Lat>=0 & Lat<=14 & Lon>=95 & Lon<=126;
Data1 = Data(id,:);

6 Comments

Is there any way to delete the NaN data for the Latitude and Longitude with NaN data?
You can use isnan to make a test, and you can use ismissing and potentially rmmissing
However, if you do that then you will no longer have a rectangular array to work with. You cannot delete scattered entries in a 2D matrix and leave "holes" in the array.

To add on to what Walter said, if you want to delete the entire rows where the third column is NaN:

id = Lat>=0 & Lat<=14 & Lon>=95 & Lon<=126;
Data1 = Data(id,:);
Data1(isnan(Data1(:,3)),:) = [];

Or don't put the NaN values in there in the first place:

id = Lat>=0 & Lat<=14 & Lon>=95 & Lon<=126 & ~isnan(SSS);
Data1 = Data(id,:);
It is not clear to me at the moment that Lat and Lon are vectors, or if they are 2D instead.
If they are vectors then I would expect that you would need to use separate masks, such as
masklat = Lat>=0 & Lat<=14;
masklon = Lon>=95 & Lon<=126;
Data1 = Data(masklat, masklon); %or maybe Data(masklon, masklat)
I tried with the delete the entire rows where the third column is NaN is work. I tried with different coding too before and it works well. Thanks Voss and Walter for the help.

Sign in to comment.

More Answers (0)

Asked:

on 25 Sep 2023

Commented:

on 25 Sep 2023

Community Treasure Hunt

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

Start Hunting!