MATLAB Answers

Opening nc file and reading sections of data

1 view (last 30 days)
Logan Tegler
Logan Tegler on 5 Aug 2021
Commented: Logan Tegler on 20 Aug 2021
Hi all --
I have an nc file with several variables I am trying to open and read at specific depths. I was able to get it to run yesterday, but today it is throwing the following error: "Index in position 1 exceeds array bounds (must not exceed 1)." This error wasn't there yesterday!
Variable Details:
LON
Size: 180x360x102
Dimensions: latitude,longitude,depth
Datatype: single
Attributes:
Description = 'longitude (degrees east)'
LAT
Size: 180x360x102
Dimensions: latitude,longitude,depth
Datatype: single
Attributes:
Description = 'latitude (degrees north)'
DEPTH
Size: 180x360x102
Dimensions: latitude,longitude,depth
Datatype: single
Attributes:
Description = 'depth (m)'
Cd_avg
Size: 180x360x102x12
Dimensions: latitude,longitude,depth,month
Datatype: single
Here is my code and thanks for any help!
filename='modelobs_v1a.nc'
ncdisp(filename);
Cdavg=ncread(filename, 'Cd_avg');
long=ncread(filename, 'LON');
lat=ncread(filename, 'LAT');
depth=ncread(filename, 'DEPTH');
firstIdx = [1 1 1 12];
lastIdx = [180 360 1 12];
t = filename('Cd_aveg', firstIdx, lastIdx); % default stride here is [1 1 1 1]
t2 = squeeze(t);
size(t2);
%t2 should show up as Cd_avg over global lat/long

Accepted Answer

Rik
Rik on 5 Aug 2021
Your second code paragraph assumes filename is a function, while the first sets it as a variable.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by