| MATLAB® | ![]() |
| On this page… |
|---|
Getting Information About CDF Files |
To get information about the contents of a Common Data Format (CDF) file, use the cdfinfo function. 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 cdfinfo function returns a structure containing general information about the file and detailed information about the variables and attributes in the file. For more information about this format, see the CDF Web site.
The following 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 Because cdfinfo creates temporary files, make sure that your current working directory is writable before attempting to use the function. |
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]
To import data into the MATLAB workspace from a Common Data Format (CDF) file, use the cdfread function. 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). Using this function, you can import all the data in the file, specific variables, specific records, or subsets of the data in a specific variable. The following examples illustrate some of these capabilities.
To get information about the contents of a CDF file, such as the names of variables in the CDF file, use the cdfinfo function. In this example, the Variables field indicates that the file contains five variables. The first variable, Time, is made up of 24 records containing CDF epoch data. The next two variables, Longitude and Latitude, have only one associated record containing int8 data. For details about how to interpret the data returned in the Variables field, see cdfinfo.
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'To read all of the data in the CDF file, use the cdfread function. The function returns the data in a 24-by-5 cell array. The five columns of data correspond to the five variables; the 24 rows correspond to the 24 records associated with the Time variable and padding elements for the rows associated with the other variables. The padding value used is specified in the CDF file.
data = cdfread('example.cdf');
whos data
Name Size Bytes Class Attributes
data 24x5 14784 cell To read the data associated with a particular variable, use the 'Variable' parameter, specifying a cell array of variable names as the value of this parameter. Variable names are case sensitive. For example, the following code reads the Longitude and Latitude variables from the file. The return value data is a 24-by-2 cell array, where each cell contains int8 data.
var_time = cdfread('example.cdf','Variable',{'Longitude','Latitude'});
whos var_time
Name Size Bytes Class Attributes
var_time 24x1 4608 cell
The cdfread function offers two ways to speed up read operations when working with large data sets:
Reducing the number of elements in the returned cell array
Returning CDF epoch values as MATLAB serial date numbers rather than as MATLAB cdfepoch objects
To reduce the number of elements in the returned cell array, specify the 'CombineRecords' parameter. By default, cdfread creates a cell array with a separate element for every variable and every record in each variable, padding the records dimension to create a rectangular cell array. For example, reading all the data from the example file produces an output cell array, 24-by-5, where the columns represent variables and the rows represent the records for each variable. When you set the 'CombineRecords' parameter to true, cdfread creates a separate element for each variable but saves time by putting all the records associated with a variable in a single cell array element. Thus, reading the data from the example file with 'CombineRecords' set to true produces a 1-by-5 cell array, as shown below.
data_combined = cdfread('example.cdf','CombineRecords',true);
whos
Name Size Bytes Class Attributes
data 24x5 14784 cell
data_combined 1x5 2364 cell
When combining records, note that the dimensions of the data in the cell change. For example, if a variable has 20 records, each of which is a scalar value, the data in the cell array for the combined element contains a 20-by-1 vector of values. If each record is a 3-by-4 array, the cell array element contains a 20-by-3-by-4 array. For combined data, cdfread adds a dimension to the data, the first dimension, that is the index into the records.
Another way to speed up read operations is to read CDF epoch values as MATLAB serial date numbers. By default, cdfread creates a MATLAB cdfepoch object for each CDF epoch value in the file. If you specify the 'ConvertEpochToDatenum' parameter, setting it to true, cdfread returns CDF epoch values as MATLAB serial date numbers. For more information about working with MATLAB cdfepoch objects, see Representing CDF Time Values.
data_datenums = cdfread('example.cdf','ConvertEpochToDatenum',true);
whos
Name Size Bytes Class Attributes
data 24x5 14784 cell
data_combined 1x5 2364 cell
var_time 24x1 4608 cell 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:
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.
m_date = todatenum(data{1});View the MATLAB serial date number as a string.
datestr(m_date) ans = 01-Jan-2001
To export data from the MATLAB workspace to a Common Data Format (CDF) file, use the cdfwrite function. 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). 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.
cdfwrite('myfile',{'Time_val',cdfepoch(now)});You can convert a cdfepoch object back into a MATLAB serial date number with the todatenum function.
![]() | Scientific Data File Formats | Flexible Image Transport System (FITS) Files | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |