Write data to HDF5 file as a dataset.
Syntax |
Description |
---|---|
h5write("filename", "dataset_name", data); |
Writes data to a dataset named "dataset_name" in an HDF5 file named "filename". Will create an HDF5 file named "filename" if it does not exist. Otherwise, the dataset is simply added to the existing HDF5 file. |
h5write("filename", "dataset_name", data, ["access_mode"]); |
Optional argument: "append" or "overwrite" "append": The dataset is added to an existing HDF5 file. If the file does not exist, it is created. If there is already an existing group in HDF5 file with the same name as “dataset_name” (for example, if one was created with h5writeattr), the dataset cannot be created. This option is set by default. "overwrite": If the HDF5 file named "filename" already exists, the file is overwritten completely. Otherwise, it will be created. The file will only contain the dataset named "dataset_name". |
h5write("filename", "dataset_name", data, [{"datatype": "datatype_name"}]); |
Optional argument. This struct indicates the data type in which the data is stored in the HDF5 file. Possible options: {“datatype”: “short”}: data is stored as short integers. {“datatype”: “int”}: data is stored as integers. {“datatype”: “long long”}: data is stored as long long integers. {“datatype”: “double”}: data is stored as doubles. |
Parameter |
Type |
Description |
---|---|---|
filename |
string |
Name of the HDF5 file. |
dataset_name |
string |
Name of the dataset. |
data |
matrix |
Real or complex data. |
access_mode |
string |
Optional argument. Only the strings "append" and "overwrite" are valid options. |
{"datatype": datatype_name} |
struct |
Optional argument. If given, the string “datatype” is a mandatory field in the struct. The field can only have the string values “short”, “int”, “long long”, and “double”. The latter is the default data type. |
Example
Write a real matrix to an HDF5 file as a dataset. Firstly, the matrix is stored using the default data type. Secondly, the matrix is stored as integer dataset.
a = [1, 2, pi; 4, 5, 2*pi]; h5write("testfile.h5", "double_matrix", a); h5write("testfile.h5", "int_matrix", a, {"datatype":"int"});
Reading the data using h5read gives the following results.
?h5read("testfile.h5", "double_matrix"); result: 1 2 3.14159 4 5 6.28319 ?h5read("testfile.h5", "int_matrix"); result: 1 2 3 4 5 6
Overwrite the existing HDF5 file by replacing the existing data with a real 1D matrix as a dataset.
b = [2, 3, 5, 7, 11, 13]; h5write("testfile.h5", "test_vector", b, "overwrite");
Applying h5info and h5read to the HDF5 file shows a singular dataset named "test_vector" which contains the given 1D matrix.
info = h5info("testfile.h5"); ?info.Datasets Cell array with 1 elements ?h5read("testfile.h5", "test_vector"); result: 2 3 5 7 11 13
See Also
List of commands , h5info , h5read , h5readattr, h5writeattr, Reading HDF5 files