Exporting to CDF Files

The Common Data Format (CDF) was created by the National Space Science Data Center (NSSDC) to provide a self-describing data storage and manipulation format that matches the structure of scientific data and applications (i.e., statistical and numerical methods, visualization, and management). For more information about this format, see the CDF Web site.

To export (write) data from a Common Data Format (CDF) file, use the MATLAB® low-level CDF functions. The MATLAB functions correspond to dozens of routines in the CDF C API library. For a complete list of all the MATLAB low-level CDF functions, see cdflib.

This section does not attempt to describe all features of the CDF library or explain basic CDF programming concepts. To use the MATLAB CDF low-level functions effectively, you must be familiar with the CDF C interface. Documentation about CDF, version 3.3.0, is available at the CDF Web site.

The following example shows how to use low-level functions to write data to a CDF file.

  1. Create a new CDF file. For information about opening an existing CDF file, see Using the CDF Library Low-Level Functions to Import Data.

    cdfid = cdflib.create('my_file.cdf');
  2. Create some variables in the CDF file.

    time_id = cdflib.createVar(cdfid,'Time','cdf_int4',1,[],true,[]);
    lat_id = cdflib.createVar(cdfid,'Latitude','cdf_int2',1,181,true,true);
    dimSizes = [20 10];
    dimVarys = [true true];
    image_id = cdflib.createVar(cdfid,'Image','cdf_int4',1,dimSizes,true,[true true]);
  3. Write data to the variables.

    % Write time data
    % Write the latitude data
    data = int16([-90:90]);
    recspec = [0 1 1];
    dimspec = { 0 181 1 };
    % Write data for the image zVariable
    recspec = [0 3 1];
    dimspec = { [0 0], [20 10], [1 1] };
    data = reshape(int32([0:599]), [20 10 3]);
  4. Create a global attribute in the CDF file and write data to the attribute..

    titleAttrNum = cdflib.createAttr(cdfid,'TITLE','global_scope');
    % Write the global attribute entries
    cdflib.putAttrEntry(cdfid,titleAttrNum,0,'CDF_CHAR','cdf Title');
  5. Create attributes associated with variables in the CDF file and write data to the attribute.

    fieldAttrNum = cdflib.createAttr(cdfid,'FIELDNAM','variable_scope');
    unitsAttrNum = cdflib.createAttr(cdfid,'UNITS','variable_scope');
    % Write the time variable attributes
    cdflib.putAttrEntry(cdfid,fieldAttrNum,time_id,'CDF_CHAR','Time of observation');
  6. Close the CDF file.

Was this topic helpful?