How to create a a scalar NetCDF variable (i.e with no dimensions)?

6 vues (au cours des 30 derniers jours)
Jonathan Blythe
Jonathan Blythe le 9 Déc 2011
Commenté : Mary Jo Brodzik le 16 Juin 2023
I need to define a NetCDF variable with a dimension of size zero. The NetCDF function, netcdf.defVar requires a dimid, but it is not possible to define a dimension of length zero, since assigning zero in netcdf.defDim produces an unlimited dimension. Does matlab have any native functions to support zero length variables?

Réponse acceptée

Ashish Uthama
Ashish Uthama le 9 Déc 2011
Could you tell us why would you need a dimension of size zero?
Are you trying to create a scalar variable? If so, try [] for the dimid in the call to netcdf.defVar. (You could also try the newer nccreate if you have it.)

Plus de réponses (1)

Jonathan Blythe
Jonathan Blythe le 12 Déc 2011
Thank you, this did the trick.
We use 'empty' NetCDF variables to tack ancillary metadata. In this case, the Climate and Forecast conventions specify a variable called Grid Mapping that is one instance of this.
  1 commentaire
Mary Jo Brodzik
Mary Jo Brodzik le 16 Juin 2023
I needed to do this same task and it took me a long time to come across this answer. One additional note for others who may also be puzzled: for the CF grid_mapping variable, you only need to define the variable and then add the projection attributes, no need to use 'putVar' to populate it with anything degenerate, e.g.:
varid = netcdf.defVar(ncid, 'crs', 'NC_INT', []);
netcdf.endDef(ncid);
netcdf.putAtt(ncid, varid, 'grid_mapping_name', 'sinusoidal');
% ...then use putAtt for the rest of the 'crs' attributes

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by