Why do I receive an error writing data to netCDF variable with the unlimited option in MATLAB 7.8 (R2009a)?

10 views (last 30 days)
I am trying to write data to a netCDF variable with infinite dimension in MATLAB 7.8 (R2009a). I execute the following code:
nc = netcdf.create('testfile.nc', netcdf.getConstant('NC_CLOBBER'));
%%define unlimited dimension
unlim_id = netcdf.defDim(nc, 'unlim', netcdf.getConstant('NC_UNLIMITED'));
%%define a dimension of length 50
dim_id = netcdf.defDim(nc, 'x', 50);
%%define a variable 'time' of unlimited dimension
t_id = netcdf.defVar(nc, 'time', 'double', unlim_id);
%%define a variable 'z' that has 2 dimensions
z_id = netcdf.defVar(nc, 'z', 'double', [dim_id unlim_id]);
netcdf.endDef(nc);
I now try putting data into variable 'time':
netcdf.putVar(nc, t_id, [1 2 3]);
or when I try putting data into variable 'z':
netcdf.putVar(nc, z_id, rand(30,20));
I get the following error: ERROR: ??? Error using ==> netcdflib The number of input elements does not match the variable size.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Aug 2019
Edited: MathWorks Support Team on 18 Aug 2019
This enhancement has been incorporated in Release 2010a (R2010a). For previous product releases, read below for any possible workarounds:
While using netCDF, when an unlimited dimension is created i.e., a dimension that can grow, the dimension length is set equal to zero by default. While growing this dimension, you need to specify the start and count. Note that if you are writing to a netCDF variable that has been defined as having N dimensions, the start and count arguments must both be N-element vectors:
netcdf.putVar(nc, t_id, 0, 3, [1 2 3]);
netcdf.putVar(nc, z_id, [0 0], [30 20], rand(30,20));

More Answers (0)

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!