This example shows how to use low-level functions to read data from a CDF file. The MATLAB® low-level CDF functions correspond to routines in the CDF C API library. To use the MATLAB CDF low-level functions effectively, you must be familiar with the CDF C interface.
Open the sample CDF File, example.cdf
.
cdfid = cdflib.open('example.cdf');
Use cdflib.inquire
to get information about the number of variables in the file, the number of global attributes, and the number of attributes with variable scope.
info = cdflib.inquire(cdfid)
info = struct with fields:
encoding: 'IBMPC_ENCODING'
majority: 'ROW_MAJOR'
maxRec: 23
numVars: 6
numvAttrs: 1
numgAttrs: 3
Use cdflib.inqurieVar
to get information about the individual variables in the file. Variable ID numbers start at zero.
info = cdflib.inquireVar(cdfid,0)
info = struct with fields:
name: 'Time'
datatype: 'cdf_epoch'
numElements: 1
dims: []
recVariance: 1
dimVariance: []
info = cdflib.inquireVar(cdfid,1)
info = struct with fields:
name: 'Longitude'
datatype: 'cdf_int1'
numElements: 1
dims: [2 2]
recVariance: 0
dimVariance: [1 0]
Read the data in a variable into the MATLAB workspace. The first variable contains CDF Epoch time values. The low-level interface returns these as double
values.
data_time = cdflib.getVarRecordData(cdfid,0,0)
data_time = 6.3146e+13
Convert the time value to a date vector.
timeVec = cdflib.epochBreakdown(data_time)
timeVec = 7×1
2001
1
1
0
0
0
0
Determine which attributes in the CDF file are global.
info = cdflib.inquireAttr(cdfid,0)
info = struct with fields:
name: 'SampleAttribute'
scope: 'GLOBAL_SCOPE'
maxgEntry: 4
maxEntry: -1
Read the value of the attribute. You must use the cdflib.getAttrgEntry
function for global attributes.
value = cdflib.getAttrgEntry(cdfid,0,0)
value = 'This is a sample entry.'
Use cdflib.close
to close the CDF file.
cdflib.close(cdfid);