Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
can't write data to the unlimited netcdf dimension

Subject: can't write data to the unlimited netcdf dimension

From: Donglai

Date: 23 Feb, 2009 07:35:09

Message: 1 of 3

Can someone explain to me why the following code of mine does not work using the builtin netcdf toolbox in Matlab 7.7+?

Thanks,
Donglai G.

%% creating a netcdf file
nc = netcdf.create('testfile.nc', netcdf.getConstant('NC_CLOBBER'));

%% define unlimited dimension
netcdf.defDim(nc, 'unlim', netcdf.getConstant('NC_UNLIMITED'));
unlim_id = netcdf.inqDimID(nc, 'unlim');

%% define a variable xx of unlimited dimension
netcdf.defVar(nc, 'xx', 'double', unlim_id);
varid = netcdf.inqVarID(nc, 'xx');
netcdf.endDef(nc);

%% put data into the variable xx
netcdf.putVar(nc, varid, [1 2 3]);

gives the following error:
??? Error using ==> netcdflib
The number of input elements does not match the variable size.

Error in ==> putVar at 76
netcdflib(funcstr,ncid,varid,varargin{:});
 
76 netcdflib(funcstr,ncid,varid,varargin{:});

Subject: can't write data to the unlimited netcdf dimension

From: John Evans

Date: 23 Feb, 2009 17:04:08

Message: 2 of 3

Hi Donglai!

When using the unlimited dimension AND if actually growing the dimension,
one must specify the start and count. What you want to do is

netcdf.putVar(nc,varid,0,3,[1 2 3]);

The error message actually makes sense if you look at things from the netCDF
library's point of view. By using the
form "netcdf.putVar(ncid,varid,data)", you are telling the library to write
to the entire netCDF variable. But the netCDF variable currently has a
length of zero, so it's expecting no data, whereas you gave it 3 elements.


Donglai wrote:

> Can someone explain to me why the following code of mine does not work
> using the builtin netcdf toolbox in Matlab 7.7+?
>
> Thanks,
> Donglai G.
>
> %% creating a netcdf file
> nc = netcdf.create('testfile.nc', netcdf.getConstant('NC_CLOBBER'));
>
> %% define unlimited dimension
> netcdf.defDim(nc, 'unlim', netcdf.getConstant('NC_UNLIMITED'));
> unlim_id = netcdf.inqDimID(nc, 'unlim');
>
> %% define a variable xx of unlimited dimension
> netcdf.defVar(nc, 'xx', 'double', unlim_id);
> varid = netcdf.inqVarID(nc, 'xx');
> netcdf.endDef(nc);
>
> %% put data into the variable xx
> netcdf.putVar(nc, varid, [1 2 3]);
>
> gives the following error:
> ??? Error using ==> netcdflib
> The number of input elements does not match the variable size.
>
> Error in ==> putVar at 76
> netcdflib(funcstr,ncid,varid,varargin{:});
>
> 76 netcdflib(funcstr,ncid,varid,varargin{:});

Subject: can't write data to the unlimited netcdf dimension

From: Donglai Gong

Date: 23 Feb, 2009 18:02:02

Message: 3 of 3

Thanks John!

That makes sense, I was being optimistic hoping that the netcdf library would be able to figure out dynamically how long the variable needs to be from the user data input. That means I have to know the length of my data array in advance before writing to the netcdf variable always? What is the use of the unlimited dimension in this case?

FYI, I'm rewritting the seagrid2roms function to be independent of netcdf_toolbox because I can't figure out how to get around it wanting to use a private method (name.m) inside ncitem. So I gave up and took the high road.

Thanks,
Donglai

John Evans <John.Evans@mathworks.com> wrote in message <gnuku8$ihk$1@fred.mathworks.com>...
> Hi Donglai!
>
> When using the unlimited dimension AND if actually growing the dimension,
> one must specify the start and count. What you want to do is
>
> netcdf.putVar(nc,varid,0,3,[1 2 3]);
>
> The error message actually makes sense if you look at things from the netCDF
> library's point of view. By using the
> form "netcdf.putVar(ncid,varid,data)", you are telling the library to write
> to the entire netCDF variable. But the netCDF variable currently has a
> length of zero, so it's expecting no data, whereas you gave it 3 elements.
>
>
> Donglai wrote:
>
> > Can someone explain to me why the following code of mine does not work
> > using the builtin netcdf toolbox in Matlab 7.7+?
> >
> > Thanks,
> > Donglai G.
> >
> > %% creating a netcdf file
> > nc = netcdf.create('testfile.nc', netcdf.getConstant('NC_CLOBBER'));
> >
> > %% define unlimited dimension
> > netcdf.defDim(nc, 'unlim', netcdf.getConstant('NC_UNLIMITED'));
> > unlim_id = netcdf.inqDimID(nc, 'unlim');
> >
> > %% define a variable xx of unlimited dimension
> > netcdf.defVar(nc, 'xx', 'double', unlim_id);
> > varid = netcdf.inqVarID(nc, 'xx');
> > netcdf.endDef(nc);
> >
> > %% put data into the variable xx
> > netcdf.putVar(nc, varid, [1 2 3]);
> >
> > gives the following error:
> > ??? Error using ==> netcdflib
> > The number of input elements does not match the variable size.
> >
> > Error in ==> putVar at 76
> > netcdflib(funcstr,ncid,varid,varargin{:});
> >
> > 76 netcdflib(funcstr,ncid,varid,varargin{:});

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us