% read_cdf.m - read the netCDF file created by make_cdf.m
[cdfid,rcode] = mexcdf('ncopen', 'test.cdf', 'NC_NOWRITE');

% get info about the file
[ndims,nvars,ngatts,recdim] = mexcdf('ncinquire',cdfid);
ndims,nvars,ngatts

% get info about dimensions
for dim = 0:ndims-1
    [dim_name,dim_size] = mexcdf('ncdiminq',cdfid,dim);
    [dimid] = mexcdf('ncdimid',cdfid,dim_name);
    dim_name,dimid
end

% get info about global attributes
for attnum = 0:ngatts-1
    [att_name] = mexcdf('ncattname',cdfid,'NC_GLOBAL',attnum);
    [att_type,att_len] = mexcdf('ncattinq',cdfid,'NC_GLOBAL',att_name);
    [values] = mexcdf('ncattget',cdfid,'NC_GLOBAL',att_name);
    att_name,values
end

% get info about variables and their attributes 
for vid = 0:nvars-1
    [var_name,var_type,nvdims,var_dim,natts] = mexcdf('ncvarinq',cdfid,vid);
    for att_num = 0:natts-1
	[att_name,rcode] = mexcdf('ncattname',cdfid,vid,att_num);
	[att_type,att_len] = mexcdf('ncattinq',cdfid,vid,att_name);
	[att_values] = mexcdf('ncattget',cdfid,var_name,att_name);
	vid,var_name,att_name,att_values
    end
end

% get info about selected variables
var = 7;
[var_name,var_type,nvdims,var_dim,natts] = mexcdf('ncvarinq',cdfid,var);
[varid] = mexcdf('ncvarid',cdfid,var_name);
[value] = mexcdf('ncvarget1',cdfid,var,[0 0 0]);
[values] = mexcdf('ncvarget',cdfid,var,[0 0 0],[3 3 3]);
var_name,value,values

% close the netCDF file
[rcode] = mexcdf('ncclose', cdfid);
