Write data to HDF5 file. The command supports only real matrices.
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. 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 group named "group_name" and the attribute named "attribute_name". |
h5write("filename", "dataset_name", data, [{"datatype": "datatype_name"}]); |
Optional argument: {"datatype": "int"} or {"datatype": "double"} Struct indicating the data type in which the data is stored in the HDF5 file. {"datatype": "int"}: Will store the matrix data as integers. {"datatype": "double"}: Will store the matrix data as doubles. |
Parameter |
Type |
Description |
---|---|---|
filename |
string |
Name of the HDF5 file |
dataset_name |
string |
Name of the dataset |
data |
matrix |
Real matrix. Will throw an error if the matrix is imaginary or complex. In this case, real and imaginary part should be stored as two separate real matrices in the HDF5 file. |
access_mode |
string |
Optional argument. Only the strings "append" and "overwrite" are valid options. |
{"datatype": datatype_name} |
struct |
Optional argument. The string "datatype" is a mandatory field in the struct. The field can only have the string values "int" and "double". The only two valid structs are {"datatype": "int"} and {"datatype": "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 , Reading HDF5 files