Programming Previous page   Next Page

Working with Common Data Format (CDF) Files

MATLAB includes functions that let you import and export data using the Common Data Format (CDF). 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). The MATLAB CDF functions are described in the following sections:

Getting Information About CDF Files

To get information about the contents of a CDF file, use the cdfinfo function. The cdfinfo function returns a structure containing general information about the file and detailed information about the variables and attributes in the file.

This example returns information about the sample CDF file included with MATLAB. To determine the variables contained in the file, view the Variables field. This field contains a cell array that lists all the variables in the file with information that describes the variable, such as name, size, and data type. For an example, see Importing Data from a CDF File.

Importing Data from a CDF File

To import data into the MATLAB workspace from a CDF file, use the cdfread function. Using this function, you can import all the data in the file, specific variables, or subsets of the data in a specific variable. This example illustrates how to use the cdfread function to read data associated with a particular variable:

  1. Determine the names of variables in the CDF file. The information returned by cdfinfo indicates that the file contains five variables.
  2. Read the data associated with the Time variable. Variable names are case sensitive.
  1. The return value data is a 24-by-1 cell array, where each cell contains a CDF epoch object.

Representing CDF Time Values

CDF represents time differently than MATLAB. CDF represents date and time as the number of milliseconds since 1-Jan-0000. This is called an epoch in CDF terminology. MATLAB represents date and time as a serial date number, which is the number of days since 0-Jan-0000. To represent CDF dates, MATLAB uses an object called a CDF epoch object. To access the time information in a CDF object, use the object's todatenum method.

For example, this code extracts the date information from a CDF epoch object:

  1. Extract the date information from the CDF epoch object returned in the cell array data (see Importing Data from a CDF File). Use the todatenum method of the CDF epoch object to get the date information, which is returned as a MATLAB serial date number.
  2. View the MATLAB serial date number as a string.

Exporting Data to a CDF File

To export data from the MATLAB workspace to a CDF file, use the cdfwrite function. Using this function, you can write variables and attributes to the file, specifying their names and associated values. See the cdfwrite reference page for more information.

This example shows how to write date information to a CDF file. Note how the example uses the CDF epoch object constructor, cdfepoch, to convert a MATLAB serial date number into a CDF epoch.

You can convert a cdfepoch object back into a MATLAB serial date number with the todatenum function.


Previous page  Working with Scientific Data Formats Working with Flexible Image Transport System (FITS) Files Next page

© 1994-2005 The MathWorks, Inc.