Main Content

Export to CDF Files

This example shows how to export data to a CDF file using MATLAB® CDF low-level functions. The MATLAB 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. Also, CDF files do not support non-ASCII encoded inputs. Therefore, variable names, attributes names, variable values, and attribute values must have 7-bit ASCII encoding.

Create New CDF File

Create a new CDF file named my_file.cdf using cdflib.create. This function corresponds to the CDF library C API routine, CDFcreateCDF.

cdfid = cdflib.create('my_file.cdf');

cdflib.create returns a file identifier, cdfid.

Create Variables in CDF File

Create variables named Time and Latitude using cdflib.createVar. This function corresponds to the CDF library C API routine, CDFcreatezVar.

time_id = cdflib.createVar(cdfid,'Time','cdf_int4',1,[],true,[]);
lat_id = cdflib.createVar(cdfid,'Latitude','cdf_int2',1,181,true,true);

cdflib.createVar returns a numeric identifier for each variable.

Create a variable named Image.

dimSizes = [20 10];
image_id = cdflib.createVar(cdfid,'Image','cdf_int4',1,...
    dimSizes,true,[true true]);

Write to Variables

Write data to the first and second records of the Time variable. Record numbers are zero-based. The cdflib.putVarRecordData function corresponds to the CDF library C API routine, CDFputzVarRecordData.


Write data to the Latitude variable.

data = int16([-90:90]);
recspec = [0 1 1];
dimspec = { 0 181 1 };

Write data to the Image variable.

recspec = [0 3 1];
dimspec = { [0 0], [20 10], [1 1] };
data = reshape(int32([0:599]), [20 10 3]);

Write to Global Attribute

Create a global attribute named TITLE using cdflib.createAttr. This function corresponds to the CDF library C API routine, CDFcreateAttr.

titleAttrNum = cdflib.createAttr(cdfid,'TITLE','global_scope');

cdflib.createAttr returns a numeric identifier for the attribute. Attribute numbers are zero-based.

Write values to entries in the global attribute.

cdflib.putAttrEntry(cdfid,titleAttrNum,0,'CDF_CHAR','cdf Title');

Write to Attributes Associated with Variables

Create attributes associated with variables in the CDF file.

fieldAttrNum = cdflib.createAttr(cdfid,'FIELDNAM','variable_scope');
unitsAttrNum = cdflib.createAttr(cdfid,'UNITS','variable_scope');

Write to attributes of the Time variable.

    'CDF_CHAR','Time of observation');

Get Information About CDF File

Get information about the file using cdflib.inquire. This function corresponds to the CDF library C API routines, CDFinquireCDF and CDFgetNumgAttributes.

info = cdflib.inquire(cdfid)
info = struct with fields:
     encoding: 'IBMPC_ENCODING'
     majority: 'ROW_MAJOR'
       maxRec: 2
      numVars: 3
    numvAttrs: 2
    numgAttrs: 1

cdflib.inquire returns a structure array that includes information about the data encoding and the number of variables and attributes in the file.

Close CDF File

Close the CDF File using cdflib.close. This function corresponds to the CDF library C API routine, CDFcloseCDF. You must close a CDF to guarantee that all modifications you made since opening the CDF are written to the file.


See Also

External Websites