Programming |
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.
Note
Before executing the cdfinfo function, make sure that your current working directory is writable because cdfinfo creates temporary files.
|
info = cdfinfo('example.cdf') info = Filename: 'example.cdf' FileModDate: '09-Mar-2001 16:45:22' FileSize: 1240 Format: 'CDF' FormatVersion: '2.7.0' FileSettings: [1x1 struct] Subfiles: {} Variables: {5x6 cell} GlobalAttributes: [1x1 struct] VariableAttributes: [1x1 struct]
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:
cdfinfo
indicates that the file contains five variables.
info = cdfinfo('example.cdf'); vars = info.Variables vars = Columns 1 through 5 'Time' [1x2 double] [24] 'epoch' 'T/' 'Longitude' [1x2 double] [ 1] 'int8' 'F/FT' 'Latitude' [1x2 double] [ 1] 'int8' 'F/TF' 'Data' [1x3 double] [ 1] 'double' 'T/TTT' 'multidimensional' [1x4 double] [ 1] 'uint8' 'T/TTTT' Column 6 'Full' 'Full' 'Full' 'Full' 'Full'
Time
variable. Variable names are case sensitive.
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:
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.
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.
Working with Scientific Data Formats | Working with Flexible Image Transport System (FITS) Files |
© 1994-2005 The MathWorks, Inc.