readnetcdf.m
author A.M. Thurnherr <athurnherr@yahoo.com>
Fri, 12 Jan 2018 17:39:32 -0500
changeset 13 82050b088a69
parent 0 40938dd7a1f1
permissions -rw-r--r--
V1.1

function prof = readnetcdf(filename)

% Usage: prof = readnetcdf(filename)
%
% Read variables from a netCDF file.

ncid = netcdf.open(filename, 'NOWRITE');

% get variables
[ndims,nvars,natts,unlimdimid] = netcdf.inq(ncid);

for varid=0:nvars-1
	[varname,xtype,dimids,nvaratts] = netcdf.inqVar(ncid,varid);
	cmd = sprintf('prof.%s = netcdf.getVar(ncid,varid);',varname);
	eval(cmd);
end

gattvarid = netcdf.getConstant('NC_GLOBAL');
for gattid=0:natts-1
	attname = netcdf.inqAttName(ncid,gattvarid,gattid);
	cmd = sprintf('prof.%s = netcdf.getAtt(ncid,gattvarid,''%s'');',attname,attname);
	eval(cmd);
end

netcdf.close(ncid)