| 1 |
gforget |
1.1 |
function ncaddVar(ncid,varname,xtype,dimlist); |
| 2 |
|
|
% 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; |
| 7 |
|
|
if isempty(useNativeMatlabNetcdf); useNativeMatlabNetcdf = ~isempty(which('netcdf.open')); end; |
| 8 |
|
|
|
| 9 |
|
|
if useNativeMatlabNetcdf; |
| 10 |
|
|
if isempty(dimlist), error('ncaddVar error: no dimension allocated'); end |
| 11 |
|
|
iDim=[]; |
| 12 |
|
|
for ii=1:length(dimlist), |
| 13 |
|
|
iDim(ii)=netcdf.inqDimID(ncid,dimlist{ii}); |
| 14 |
|
|
end |
| 15 |
|
|
netcdf.reDef(ncid); |
| 16 |
|
|
netcdf.defVar(ncid,varname,xtype,iDim); |
| 17 |
|
|
netcdf.endDef(ncid); |
| 18 |
|
|
else;%try to use old mex stuff |
| 19 |
|
|
% inverse the order of list dimensions |
| 20 |
|
|
dimlist=fliplr(dimlist); |
| 21 |
|
|
switch length(dimlist) |
| 22 |
|
|
case 1, |
| 23 |
|
|
eval(sprintf('ncid{''%s''}=nc%s(''%s'');',varname,xtype,dimlist{1})); |
| 24 |
|
|
case 2, |
| 25 |
|
|
eval(sprintf('ncid{''%s''}=nc%s(''%s'',''%s'');',varname,xtype,dimlist{1},dimlist{2})); |
| 26 |
|
|
case 3, |
| 27 |
|
|
eval(sprintf('ncid{''%s''}=nc%s(''%s'',''%s'',''%s'');',varname,xtype,dimlist{1},dimlist{2},dimlist{3})); |
| 28 |
|
|
otherwise |
| 29 |
|
|
error('ncaddVar: number of dimension > 3'); |
| 30 |
|
|
end |
| 31 |
|
|
end; |
| 32 |
|
|
|
| 33 |
|
|
|