Main Content

Import CDF Files Using High-Level Functions

This example shows how to use high-level MATLAB® functions to import the sample CDF file, example.cdf. High-level functions provide a simpler interface to accessing CDF files.

Get Information About Contents of CDF File

Get information about the contents of a CDF file using the cdfinfo function. Because cdfinfo creates temporary files, ensure that your current folder is writable before using the function.

info = cdfinfo('example.cdf')
info = struct with fields:
              Filename: 'example.cdf'
           FileModDate: '10-May-2010 21:35:01'
              FileSize: 1310
                Format: 'CDF'
         FormatVersion: '2.7.0'
          FileSettings: [1x1 struct]
              Subfiles: {}
             Variables: {6x6 cell}
      GlobalAttributes: [1x1 struct]
    VariableAttributes: [1x1 struct]

cdfinfo returns a structure containing general information about the file and detailed information about the variables and attributes in the file. In this example, the Variables field indicates the number of variables in the file.

View the contents of the Variables field.

vars = info.Variables
vars=6×6 cell array
    {'Time'            }    {[    1 1]}    {[24]}    {'epoch' }    {'T/'    }    {'Full'}
    {'Longitude'       }    {[    2 2]}    {[ 1]}    {'int8'  }    {'F/FT'  }    {'Full'}
    {'Latitude'        }    {[    2 2]}    {[ 1]}    {'int8'  }    {'F/TF'  }    {'Full'}
    {'Data'            }    {[  2 2 4]}    {[ 1]}    {'double'}    {'T/TTT' }    {'Full'}
    {'multidimensional'}    {[2 2 3 4]}    {[ 1]}    {'uint8' }    {'T/TTTT'}    {'Full'}
    {'Temperature'     }    {[    3 2]}    {[10]}    {'int16' }    {'T/TT'  }    {'Full'}

The first variable, Time, consists of 24 records containing CDF epoch data. The next two variables, Longitude and Latitude, each have only one associated record containing int8 data.

Read All Data from CDF File

Use the cdfread function to read all of the data in the CDF file.

data = cdfread('example.cdf');
whos data
  Name       Size            Bytes  Class    Attributes

  data      24x6             23904  cell               

cdfread returns the data in a cell array. The columns of data correspond to the variables. The rows correspond to the records associated with a variable.

Read Data from Specific Variables

Read only the Longitude and Latitude variables from the CDF file. To read the data associated with particular variables, use the 'Variable' parameter. Specify the names of the variables in a cell array of character vectors. Variable names are case sensitive.

var_long_lat = cdfread('example.cdf','Variable',{'Longitude','Latitude'});
whos var_long_lat
  Name              Size            Bytes  Class    Attributes

  var_long_lat      1x2               216  cell               

Combine Records to Speed Up Read Operations

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. When working with large data sets, you can speed up read operations by specifying the 'CombineRecords' parameter to reduce the number of elements in the cell array that cdfread returns. When you set the 'CombineRecords' parameter to true, the cdfread function 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.

data_combined = cdfread('example.cdf','CombineRecords',true);

Compare the sizes of the cell arrays returned by cdfread.

whos data*
  Name                Size            Bytes  Class    Attributes

  data               24x6             23904  cell               
  data_combined       1x6              8080  cell               

Reading all the data from the example file without the CombineRecords parameter returns a 24-by-6 cell array, where the columns represent variables and the rows represent the records for each variable. Reading the data from the same file with 'CombineRecords' set to true returns a 1-by-6 cell array.

When combining records, the dimensions of the data in the cell change. In this example, the Time variable has 24 records, each of which is a scalar value. In the data_combined cell array, the combined element contains a 24-by-1 vector of values.

Read CDF Epoch Values as Serial Date Numbers

By default, cdfread creates a MATLAB cdfepoch object for each CDF epoch value in the file. Speed up read operations by setting the 'ConvertEpochToDatenum' name-value pair argument to true, to return CDF epoch values as MATLAB serial date numbers.

data_datenums = cdfread('example.cdf','ConvertEpochToDatenum',true);
whos data*
  Name                Size            Bytes  Class    Attributes

  data               24x6             23904  cell               
  data_combined       1x6              8080  cell               
  data_datenums      24x6             19872  cell               

See Also


External Websites