Main Content

Import CDF Files Using High-Level Functions

This example uses high-level functions to import a CDF file. The MATLAB® high-level functions provide a simple interface for 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_364.cdf")
info = struct with fields:
              Filename: 'example_364.cdf'
           FileModDate: '08-Jul-2022 11:07:41'
              FileSize: 1860
                Format: 'CDF'
         FormatVersion: '3.6.4'
          FileSettings: [1x1 struct]
              Subfiles: {}
             Variables: {8x6 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=8×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'}
    {'multiInt8'       }    {[    2 3]}    {[ 2]}    {'int64' }    {'T/TT'  }    {'Full'}
    {'tt2000'          }    {[    1 1]}    {[ 8]}    {'tt2000'}    {'T/'    }    {'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_364.cdf");
whos data
  Name       Size            Bytes  Class    Attributes

  data      24x8             32608  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 specific variables, use the Variables name-value argument. Specify the names of the variables in a cell array of strings. Variable names are case sensitive.

var_long_lat = cdfread("example_364.cdf","Variables",{"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 name-value argument to reduce the number of elements in the cell array that cdfread returns. When you set the CombineRecords argument 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_364.cdf","CombineRecords",true);

Compare the sizes of the cell arrays returned by cdfread.

whos data*
  Name                Size            Bytes  Class    Attributes

  data               24x8             32608  cell               
  data_combined       1x8              9920  cell               

Reading all the data from the example file without the CombineRecords argument returns a 24-by-8 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-8 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 and CDF_TIME_TT2000 Values as datetime Values

Set the DatetimeType name-value argument to "datetime" to return both CDF_EPOCH and CDF_TIME_TT2000 values as MATLAB datetime values.

data_datetimes = cdfread("example_364.cdf","DatetimeType","datetime");
whos data*
  Name                 Size            Bytes  Class    Attributes

  data                24x8             32608  cell               
  data_combined        1x8              9920  cell               
  data_datetimes      24x8             29728  cell               

See Also

|

External Websites