23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Hierarchical Data Format (HDF5) Files<br />

1 Create the <strong>MATLAB</strong> variable that you want to write to the HDF5 file. The<br />

examples creates a three-dimensional array of uint8 data.<br />

testdata = uint8(ones(5,10,3));<br />

2 CreatetheHDF5fileoropenanexistingfile.Theexamplecreatesanew<br />

HDF5 file, named my_file.h5, in the system temp directory.<br />

filename = fullfile(tempdir,'my_file.h5');<br />

fileID = H5F.create(filename,'H5F_ACC_TRUNC','H5P_DEFAULT','H5P_DEFAULT');<br />

In HDF5, you use the H5Fcreate function to create a file. The example<br />

uses the <strong>MATLAB</strong> equivalent, H5F.create. As arguments, specify the<br />

name you want to assign to the file, the type of access you want to the file<br />

('H5F_ACC_TRUNC' in the example), and optional additional characteristics<br />

specified by a file creation property list and a file access property list. This<br />

example uses default values for these property lists ('H5P_DEFAULT').<br />

Intheexample,notehowtheCconstantsarepassedtothe<strong>MATLAB</strong><br />

functions as strings. The function returns an ID to the HDF5 file.<br />

3 Create the data set in the file to hold the <strong>MATLAB</strong> variable. In the HDF5<br />

programming model, you must define the data type and dimensionality<br />

(dataspace)ofthedatasetasseparateentities.<br />

a Specify the data type used by the data set. In HDF5, you use the<br />

H5Tcopy function to create integer or floating-point data types. The<br />

example uses the corresponding <strong>MATLAB</strong> function, H5T.copy, to create<br />

a uint8 data type because the <strong>MATLAB</strong> data is uint8. The function<br />

returns a data type ID.<br />

datatypeID = H5T.copy('H5T_NATIVE_UINT8');<br />

b Specify the dimensions of the data set. In HDF5, you use the<br />

H5Screate_simple routinetocreateadataspace.Theexampleusesthe<br />

corresponding <strong>MATLAB</strong> function, H5S.create_simple, tospecifythe<br />

dimensions. The function returns a data space ID.<br />

dims(1) = 5;<br />

dims(2) = 10;<br />

dims(3) = 3<br />

dataspaceID = H5S.create_simple(3, dims, []);<br />

7-31

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!