Cannot read NC file dimensions values and regrid them

2 views (last 30 days)
Hello,
I'm trying tot rescale coordinates in the .nc file that has one variable.
ncfile = 'my_file.nc';
ncdisp(ncfile) produces this result;
Format: netcdf4_classic
Dimensions:
lat = 234
lon = 288
Variables:
var
Size: 234x288
Dimensions: lat,lon
Datatype: double
info = ncinfo(ncfile);
dim_lat = info.Variables(strcmp({info.Variables.Name}, 'swr')).Dimensions(strcmp({info.Variables(strcmp({info.Variables.Name}, 'var')).Dimensions.Name}, 'lat')).Name;
dim_lon = info.Variables(strcmp({info.Variables.Name}, 'swr')).Dimensions(strcmp({info.Variables(strcmp({info.Variables.Name}, 'var')).Dimensions.Name}, 'lon')).Name;
But then I'm getting an error when I'm trying to read the values:
lat_existing = ncread(ncfile, dim_lat);
lon_existing = ncread(ncfile, dim_lon);
Error using internal.matlab.imagesci.nc/getGroupAndVarid
Could not find variable or group 'lat' in file.
Error in internal.matlab.imagesci.nc/read (line 648)
[gid, varid] = getGroupAndVarid(this, location);
Error in ncread (line 76)
vardata = ncObj.read(varName, varargin{:});

Accepted Answer

MJFcoNaN
MJFcoNaN on 28 Feb 2024
Edited: MJFcoNaN on 28 Feb 2024
Hello,
"ncread" can only get a variable not a dimention.
There is only a length of any "Dimention", such as lat=234, but no value.
In many cases, you will find some variables under the same or different name with the dimentions, then you can use ncread to get them. For my fake example, ncread(ncfile, "lat"), ncread(ncfile, "longitude") and ncread(ncfile, "lat_matrix") will get the value:
Format: netcdf4_classic
Dimensions:
lat = 234
lon = 288
Variables:
var
Size: 234x288
Dimensions: lat,lon
Datatype: double
lat
Size: 234
Dimensions: lat
Datatype: double
longitude
Size: 288
Dimensions: lon
Datatype: double
lat_matrix
Size: 234x288
Dimensions: lat,lon
Datatype: double

More Answers (0)

Categories

Find more on Triangulation Representation 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!