1 function p=ncread2struct(filename,varnames,gattnames,p)
2 % p=ncread2str(filename,vars,gatts)
3 % read from netcdf file to structure
7 % filename string, name of file to read
8 % varnames cell array of strings, names of variable to read
9 % gattnames cell array of strings, names of global attributes to read
10 % p (optional) the structure to add the fields to
12 % p matlab structure with the specifed variables and attributes as fields
13 % NOTE: the values types are kept and dimensions are not squeezed
16 % p=ncread2struct('wrfinput_d01',{'U','V'},{'DX','DY'})
17 % will read variables U,V into p.U, p.V and global attributes DX DY into
18 % p.DX p.DY, respectively
20 p.filename{1}=filename;
22 for i=1:length({varnames{:}}),
24 v=ncvar(filename,varname);
25 p.(varname)=v.var_value;
28 for i=1:length({gattnames{:}}),
29 gattname=gattnames{i};
30 v=ncgetgatt(filename,gattname);