1 |
function ncaddVar(ncid,varname,xtype,dimlist); |
function ncaddVar(ncid,varname,xtype,dimlist); |
2 |
% add a variable in an existing netcdf file. |
% function ncaddVar(ncid,varname,xtype,dimlist); |
3 |
|
% add a variable in an existing netcdf file. |
4 |
|
% The netcdf file must be open in 'write' mode. |
5 |
|
|
6 |
global useNativeMatlabNetcdf; |
global useNativeMatlabNetcdf; |
7 |
if isempty(useNativeMatlabNetcdf); useNativeMatlabNetcdf = ~isempty(which('netcdf.open')); end; |
if isempty(useNativeMatlabNetcdf); useNativeMatlabNetcdf = ~isempty(which('netcdf.open')); end; |
8 |
|
|
9 |
if useNativeMatlabNetcdf; |
if useNativeMatlabNetcdf; |
10 |
if isempty(dimlist), error('nddefVar error: no dimensions allocated'); end |
if isempty(dimlist), error('ncaddVar error: no dimension allocated'); end |
11 |
iDim=[]; |
iDim=[]; |
12 |
for ii=1:length(dimlist), |
for ii=1:length(dimlist), |
13 |
iDim(ii)=netcdf.inqDimID(ncid,dimlist{ii}); |
iDim(ii)=netcdf.inqDimID(ncid,dimlist{ii}); |
20 |
dimlist=fliplr(dimlist); |
dimlist=fliplr(dimlist); |
21 |
switch length(dimlist) |
switch length(dimlist) |
22 |
case 1, |
case 1, |
23 |
sprintf('ncid{''%s''}=nc%s(''%s'');',varname,xtype,dimlist{1}); |
eval(sprintf('ncid{''%s''}=nc%s(''%s'');',varname,xtype,dimlist{1})); |
24 |
case 2, |
case 2, |
25 |
sprintf('ncid{''%s''}=nc%s(''%s'',''%s'');',varname,xtype,dimlist{1},dimlist{2}); |
eval(sprintf('ncid{''%s''}=nc%s(''%s'',''%s'');',varname,xtype,dimlist{1},dimlist{2})); |
26 |
case 3, |
case 3, |
27 |
sprintf('ncid{''%s''}=nc%s(''%s'',''%s'',''%s'');',varname,xtype,dimlist{1},dimlist{2},dimlist{3}); |
eval(sprintf('ncid{''%s''}=nc%s(''%s'',''%s'',''%s'');',varname,xtype,dimlist{1},dimlist{2},dimlist{3})); |
28 |
otherwise |
otherwise |
29 |
error('ncdefVar: number of dimension > 3'); |
error('ncaddVar: number of dimension > 3'); |
30 |
end |
end |
31 |
end; |
end; |
32 |
|
|