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