Creating netcdf file error

I am trying to create a .nc file and with the following script as my example:
% Intialise file
nc=netcdf('my_file.nc','clobber');
% Define DIMS
ncdim('LATS',length(Y),nc);
ncdim('LONS',length(X),nc);
ncvar('LATS','double',{'LATS'},nc);
nc{'LATS'}(:) = Y';
Doing this i get the error:
??? Error: File: /home/matlab/toolbox/netcdf/@ncvar/subsasgn.m Line: 118 Column: 9 "subs" previously appeared to be used as a function or command, conflicting with its use here as the name of a variable. A possible cause of this error is that you forgot to initialize the variable, or you have initialized it implicitly using load or eval.
Error in ==> netcdf.subsasgn at 83 result = subsasgn(v, s, other);
Any help?? I cannot seem to assign values to variable that I am trying to create.

 Respuesta aceptada

Dave Correa
Dave Correa el 24 de Jun. de 2011

0 votos

Hello;
I'm leaving you an example.
I hope this is of some help.
Regards
Dave Correa
E-mail:correa.dave30@gmail.com
close all, clear all, clc
yearini=1960;
yearfin=1970;
%%--------------------
nt=144;
nx=1440;
ny=720;
lon=linspace(-180,180,nx);
lat=linspace(90,-90,ny);
time=1:nt;
[mlat,mlon]=meshgrid(lat,lon);
disp('Creando file Netcdf...')
filenc=['Data.nc'];
ncid = netcdf.create(filenc,'NC_WRITE');
% Crear dimensiones
dimid_lon = netcdf.defDim(ncid,'longitude',nx);
dimid_lat = netcdf.defDim(ncid,'latitude',ny);
dimid_time = netcdf.defDim(ncid,'time',nt);
% Crear variables y atributos
varid_lon = netcdf.defVar(ncid,'longitude','double',dimid_lon);
netcdf.putAtt(ncid,varid_lon,'long_name','Longitude')
netcdf.putAtt(ncid,varid_lon,'units','degrees_east')
%
varid_lat = netcdf.defVar(ncid,'latitude','double',dimid_lat);
netcdf.putAtt(ncid,varid_lat,'long_name','Latitude')
netcdf.putAtt(ncid,varid_lat,'units','degrees_north')
%
varid_time = netcdf.defVar(ncid,'time','double',dimid_time);
netcdf.putAtt(ncid,varid_time,'long_name','Time')
netcdf.putAtt(ncid,varid_time,'units','Months since 1960-01-01 00:00:00')
%
varid_prec = netcdf.defVar(ncid,'prec','double',[dimid_lon,dimid_lat,dimid_time]);
netcdf.putAtt(ncid,varid_prec,'long_name','Variable')
netcdf.putAtt(ncid,varid_prec,'units','mm')
netcdf.putAtt(ncid,varid_prec,'missing_value',-9999)
netcdf.endDef(ncid)
% % % Agregar datos de coordenadas
netcdf.putVar(ncid,varid_lon,lon);
netcdf.putVar(ncid,varid_lat,lat);
netcdf.putVar(ncid,varid_time,time);
m=1;
for year=yearini:yearfin
for mes=1:12
netcdf.putVar(ncid,varid_prec,[0 0 m-1],[nx ny 1],tmp);
m=m+1;
end
end
netcdf.close(ncid)

3 comentarios

Evan
Evan el 27 de Jun. de 2011
Hi Dave,
Thanks very much for the reply. One more question is whether or not you can assign cell or char array to a nc variable.
For example, I currently have a cell array of size 25 x 1 named titles consisting of 25 titles that I want to save in the netcdf file. If I convert this to char the dims now are 25 x 14.
I was trying to assign the variable as such:
ncdim('TITLES',length(titles),ncfile); % length(titles) is 25
nc{'TITLES'}=ncchar('TITLES'); nc{'TITLES'}(:)=titles;
But received this message:
## Conversion to double from cell is not possible.
So I tried:
nc{'TITLES'}(:)=char(titles);
But received this message:
??? In an assignment A(:) = B, the number of elements in A and B must be the same.
This is I assume because the dims are now 25 x 14?
How do I assign cell or char arrays to a netcdf variable
Walter Roberson
Walter Roberson el 27 de Jun. de 2011
You are unlikely to be able to use a string as a cell array subscript. Notice that Dave never uses any syntax like that.
Ashish Uthama
Ashish Uthama el 1 de Jul. de 2011
Evan, I believe you are using a 3rd party toolbox. Dave's answer uses MATLAB's internal netcdf package. It does not support {} indexing (its a thin wrapper around the netcdf library from Unidata).

Iniciar sesión para comentar.

Más respuestas (1)

Dave Correa
Dave Correa el 8 de Jul. de 2011

0 votos

Dear Evan;
I'm using the native NetCDF toolbox Matlab. For your question, Yes, you can use text in a cell to create variable names. I leave you an example.
I hope this is of some help.
regards
Dave Correa
E-mail:correa.dave30@gmail.com
Example:
clc,close all,clear all
% Create file NetCDF.
nx=320; % Length of Longitud
ny=639; % Length of Latitude
nt=12; % Length of Time
disp('Create file Netcdf...')
filenc=['example_nc.nc'];
ncid = netcdf.create(filenc,'NC_WRITE');
% Create dimensions
dimid_lon = netcdf.defDim(ncid,'longitude',nx);
dimid_lat = netcdf.defDim(ncid,'latitude',ny);
dimid_time = netcdf.defDim(ncid,'time',nt);
% Create variables y atributtes of referencia Spacial and time
varid_lon = netcdf.defVar(ncid,'longitude','double',dimid_lon);
netcdf.putAtt(ncid,varid_lon,'long_name','Longitude')
netcdf.putAtt(ncid,varid_lon,'units','degrees_east')
varid_lat = netcdf.defVar(ncid,'latitude','double',dimid_lat);
netcdf.putAtt(ncid,varid_lat,'long_name','Latitude')
netcdf.putAtt(ncid,varid_lat,'units','degrees_north')
varid_time = netcdf.defVar(ncid,'time','double',dimid_time);
netcdf.putAtt(ncid,varid_time,'long_name','Time')
netcdf.putAtt(ncid,varid_time,'units','Days since 1981-09-27 00:00:00')
% Define cell with variable name
variable={'var_01','var_02','var_03','var_04','var_05','var_06','var_07',...
'var_08','var_09','var_10','var_11','var_12','var_13','var_14','var_15'};
for n=1:length(variable)
vari=variable{n}; % Save name temporal of n variable
% Create variable n
varid_var = netcdf.defVar(ncid,vari,'double',[dimid_lon,dimid_lat,dimid_time]);
netcdf.putAtt(ncid,varid_var,'long_name',vari)
netcdf.putAtt(ncid,varid_var,'units','[Unit]')
netcdf.putAtt(ncid,varid_var,'missing_value',-9999)
% Assign values to variables
% In this section we assign values to each of the variables
%
end
netcdf.endDef(ncid) % Finaly definition of variables
netcdf.close(ncid) % Close file NetCDF.
% Verificate NetCDF file create
ncid = netcdf.open('example_nc.nc','NOWRITE');
[ndims,nvars,ngatts,unlimdimid] = netcdf.inq(ncid);
nvars

Preguntada:

el 24 de Jun. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by