Import CDF Files Using Low-Level Functions

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 CDF File

Open the sample CDF File, example.cdf.

cdfid ='example.cdf');

Get Information About File Contents

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 = 

     encoding: 'IBMPC_ENCODING'
     majority: 'ROW_MAJOR'
       maxRec: 23
      numVars: 6
    numvAttrs: 1
    numgAttrs: 3

Get Information About Variables

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 = 

           name: 'Time'
       datatype: 'cdf_epoch'
    numElements: 1
           dims: []
    recVariance: 1
    dimVariance: []

info  = cdflib.inquireVar(cdfid,1)
info = 

           name: 'Longitude'
       datatype: 'cdf_int1'
    numElements: 1
           dims: [2 2]
    recVariance: 0
    dimVariance: [1 0]

Read Variable Data Into Workspace

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 =


Convert the time value to a date vector.

timeVec = cdflib.epochBreakdown(data_time)
timeVec =


Read Global Attribute From File

Determine which attributes in the CDF file are global.

info = cdflib.inquireAttr(cdfid,0)
info = 

         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.

Close CDF File

Use cdflib.close to close the CDF file.


See Also


External Websites

Was this topic helpful?