%% Write Data to HDF5 Data Set Using MATLAB Low-Level Functions
% This example shows how to use the MATLAB® HDF5 low-level functions to write a data set to an HDF5 file and then read the data set from the file.
% Create a 2-by-3 array of data to write to an HDF5 file.

testdata = [1 3 5; 2 4 6];

%
% Create a new HDF5 file named my_file.h5 in the system temp folder. Use the MATLAB H5F.create function to create a file. This MATLAB function corresponds to the HDF5 function, H5Fcreate. As arguments, specify the name you want to assign to the file, the type of access you want to the file ('H5F_ACC_TRUNC' in this case), and optional additional characteristics specified by a file creation property list and a file access property list. In this case, use default values for these property lists ('H5P_DEFAULT'). Pass C constants to the MATLAB function as character vectors.

filename = fullfile(tempdir,'my_file.h5');
fileID = H5F.create(filename,'H5F_ACC_TRUNC','H5P_DEFAULT','H5P_DEFAULT');

% H5F.create returns a file identifier corresponding to the HDF5 file.
%
% Create the data set in the file to hold the MATLAB variable. In the HDF5 programming model, you must define the data type and dimensionality (data space) of the data set as separate entities. First, use the H5T.copy function to specify the data type used by the data set, in this case, double. This MATLAB function corresponds to the HDF5 function, H5Tcopy.

datatypeID = H5T.copy('H5T_NATIVE_DOUBLE');

% H5T.copy returns a data type identifier.
%
% Create a data space using H5S.create_simple, which corresponds to the HDF5 function, H5Screate_simple. The first input, 2, is the rank of the data space. The second input is an array specifying the size of each dimension of the dataset. Because HDF5 stores data in row-major order and the MATLAB array is organized in column-major order, you should reverse the ordering of the dimension extents before using H5Screate_simple to preserve the layout of the data. You can use fliplr for this purpose.

dims = size(testdata);
dataspaceID = H5S.create_simple(2,fliplr(dims),[]);

% H5S.create_simple returns a data space identifier, dataspaceID. Note that other software programs that use row-major ordering (such as H5DUMP from the HDF Group) might report the size of the dataset to be 3-by-2 instead of 2-by-3.
%
% Create the data set using H5D.create, which corresponds to the HDF5 function, H5Dcreate. Specify the file identifier, the name you want to assign to the data set, the data type identifier, the data space identifier, and a data set creation property list identifier as arguments. 'H5P_DEFAULT' specifies the default property list settings.

dsetname = 'my_dataset';  
datasetID = H5D.create(fileID,dsetname,datatypeID,dataspaceID,'H5P_DEFAULT');

% H5D.create returns a data set identifier, datasetID.
%
% Write the data to the data set using H5D.write, which corresponds to the HDF5 function, H5Dwrite. The input arguments are the data set identifier, the memory data type identifier, the memory space identifier, the data space identifier, the transfer property list identifier and the name of the MATLAB variable to write to the data set. The constant, 'H5ML_DEFAULT', specifies automatic mapping to HDF5 data types. The constant, 'H5S_ALL', tells H5D.write to write all the data to the file.

H5D.write(datasetID,'H5ML_DEFAULT','H5S_ALL','H5S_ALL',...
    'H5P_DEFAULT',testdata);

%
% Close the data set, data space, data type, and file objects. If used inside a MATLAB function, these identifiers are closed automatically when they go out of scope.

H5D.close(datasetID);
H5S.close(dataspaceID);
H5T.close(datatypeID);
H5F.close(fileID);

%
% Open the HDF5 file in order to read the data set you wrote. Use H5F.open to open the file for read-only access. This MATLAB function corresponds to the HDF5 function, H5Fopen.

fileID = H5F.open(filename,'H5F_ACC_RDONLY','H5P_DEFAULT');

%
% Open the data set to read using H5D.open, which corresponds to the HDF5 function, H5Dopen. Specify as arguments the file identifier and the name of the data set, defined earlier in the example.

datasetID = H5D.open(fileID,dsetname);

%
% Read the data into the MATLAB workspace using H5D.read, which corresponds to the HDF5 function, H5Dread. The input arguments are the data set identifier, the memory data type identifier, the memory space identifier, the data space identifier, and the transfer property list identifier.

returned_data = H5D.read(datasetID,'H5ML_DEFAULT',...
    'H5S_ALL','H5S_ALL','H5P_DEFAULT');

%
% Compare the original MATLAB variable, testdata, with the variable just created, returned_data.

isequal(testdata,returned_data)

% The two variables are the same.
% Copyright 2012 The MathWorks, Inc.