Subscript indices must either be real positive integers or logicals.

3 views (last 30 days)
Hi I tried to run this coding, but always getting error which is subscripts indices must either be real positive integers or logicals.
Here is my coding, I also attach the file starting from Data1 line.
clear all
clc
ncfile = 'filename.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
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(:)];
id = (Lat>=0&Lat<=14);
Data1 = Data(id,:,:);
X = Data1(:,1);
Y = Data1(:,2);
Z = Data1(:,3);
Lat1 = X;
Lon1 = Y;
SSS1 = Z;
n = length(Lat1);
minlat = 1;
maxlat = 14;
minlon = 95;
maxlon = 126;
% gridsize = 0.125;
[XX,YY] = meshgrid(linspace(minlat(Lat1),maxlat(Lat1),n),linspace(minlon(Lon1),maxlon(Lon1),n));
zz = surfergriddata(X,Y,Z,XX,YY, 'kriging')
Here is the error
Subscript indices must either be real positive integers or logicals.
Error in Copy_of_SMOS_nc (line 36)
[XX,YY] = meshgrid(linspace(minlat(Lat1),maxlat(Lat1),n),linspace(minlon(Lon1),maxlon(Lon1),n));

Answers (2)

VBBV
VBBV on 29 Mar 2023
Edited: VBBV on 29 Mar 2023
[XX,YY] = meshgrid(linspace((Lat1(minlat),(Lat1(maxlat),n),linspace((Lon1(minlon)),(Lon1(maxlon),n))
  2 Comments
VBBV
VBBV on 29 Mar 2023
Edited: VBBV on 29 Mar 2023
Use the linspace command this way for Meshgrid command.
VBBV
VBBV on 29 Mar 2023
Edited: VBBV on 29 Mar 2023
% search the minlat , maxlat minlon & maxlon in the Arrays Lat1, Lon1 and
% input to linspace,meshgrid commands if thats what you actually need
LaMi = Lat1(Lat1 == minlat);
LaMa = Lat1(Lat1 == maxlat);
LoMi = Lon1(Lon1 == minlon);
LoMa = Lon1(Lon1 == maxlon);
[XX,YY] = meshgrid(linspace(Lat1(LaMi(1)),Lat1(LaMa(1)),n),linspace(Lon1(LoMi(1)),Lon1(LoMa(1)),n))

Sign in to comment.


Image Analyst
Image Analyst on 29 Mar 2023

Categories

Find more on Special Functions 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!