MATLAB: Characters in netcdf.defVar

define dimensionsnetcdfwriting strings

Hi, I want to write a string to netcdf. I can write NC_DOUBLE without problems. So far I have:
ncid = netcdf.create(ncfullfilename ,'NC_WRITE');
heightsteps = netcdf.defDim(ncid,'rows',finaldimen(1));
timesteps = netcdf.defDim(ncid,'length',finaldimen(2));
varidH = netcdf.defVar(ncid,'Height','NC_DOUBLE',[heightsteps 1]);
varidP355 = netcdf.defVar(ncid,'P355','NC_DOUBLE',[heightsteps timesteps]);
% all this works fine, now:
varidTime = netcdf.defVar(ncid,'Time','NC_CHAR',[timesteps 20]);
>>> Error using netcdflib
The NetCDF library encountered an error during execution of 'defVar' function – 'Invalid dimension ID or
name (NC_EBADDIM)'.
Error in netcdf.defVar (line 38)
varid = netcdflib('defVar', ncid, varname, xtype, dimids);
However, I can read in just 1 character by:
varidTime = netcdf.defVar(ncid,'Time','NC_CHAR',1)
"Time" may look like this: 20-Jun-2017-06:14:50
Can you tell me what I do wrong?
Thanks and best regards
Christoph Ritter

Best Answer

The parameters passed in defVar need to be the dimension identifiers. You do not have a dimension identifier of 20 available to you: you have 0 (heightsteps) and 1 (timesteps).
If you are trying to make a something-by-20 array then you could create a new dimension name that had the value 20 and use the id for it where you have the 20.
Watch out in your definition for varidH : you use the hardcoded dimension identifier 1, which in this sequence would happen to correspond to timesteps, same as on your P355 definition.