%% 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')

% 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

% 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

% 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

%
%% 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*

% 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*

% Copyright 2012 The MathWorks, Inc.