Main Content

Import CDF Files Using Low-Level Functions

This example uses low-level functions to read data from a CDF file. The MATLAB® CDF low-level 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.

cdfid ="example_364.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.

globalInfo = cdflib.inquire(cdfid)
globalInfo = struct with fields:
     encoding: 'IBMPC_ENCODING'
     majority: 'ROW_MAJOR'
       maxRec: 23
      numVars: 8
    numvAttrs: 3
    numgAttrs: 5

Get Information About Variables

Use cdflib.inquireVar to get information about the individual variables in the file. Variable ID numbers start at zero.

var0Info = cdflib.inquireVar(cdfid,0)
var0Info = struct with fields:
           name: 'Time'
       datatype: 'cdf_epoch'
    numElements: 1
           dims: []
    recVariance: 1
    dimVariance: []

var1Info = cdflib.inquireVar(cdfid,1)
var1Info = struct with fields:
           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 values as type double.

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


Read Global Attribute from File

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.'

Close CDF File

Use cdflib.close to close the CDF file.


See Also

| | | | |

External Websites