Write Strings to Existing HDF5 file
Show older comments
Hello, I am having difficulty writing an array of string data to an existing HDF5 file. I am able to write successfully to a new file using the HDF5 Matlab example (h5 ex_t_string.m, available at https://support.hdfgroup.org/HDF5/examples/api18-m.html), but when I modify this code to write to a new dataset within an existing HDF5 file, a random sequence of characters is generated.
My code is as follows (taken mostly from the example, but modified to write to an existing instead of new hdf5 file):
filename = 'rptestiso.h5'
%Assign variables for data to be written and array dimensions (taken directly from example code)
wdata = ['Parting'; 'is such'; 'sweet '; 'sorrow.'];
DIM0 = size(wdata,1);
SDIM = size(wdata,2)+1;
dims = DIM0;
%Open file using Read/Write option
file_id = H5F.open(filename,'H5F_ACC_RDWR','H5P_DEFAULT');
% Preceeding line replaces the H5F create statement in the example:
%file = H5F.create (filename, 'H5F_ACC_TRUNC', 'H5P_DEFAULT', 'H5P_DEFAULT');
%Create file and memory datatypes
filetype = H5T.copy ('H5T_C_S1');
H5T.set_size (filetype, SDIM-1);
memtype = H5T.copy ('H5T_C_S1');
H5T.set_size (memtype, SDIM-1);
% Create dataspace. Setting maximum size to [] sets the maximum
% size to be the current size.
%
space_id = H5S.create_simple (1,fliplr( dims), []);
% Create the dataset and write the string data to it.
%
dataset_id = H5D.create (file_id, 'DateTimeString', filetype, space_id, 'H5P_DEFAULT');
% Transpose the data to match the layout in the H5 file to match C
% generated H5 file.
H5D.write (dataset_id, memtype, 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT', wdata);
%Close and release resources
H5D.close(dataset_id);
H5S.close(space_id);
H5T.close(filetype);
H5T.close(memtype);
H5F.close(file_id);
The output that is produced by the example in a new HDF5 file (and what I expect to be written to my existing HDF5 file) looks like this:

The output produced by my code in the existing hdf5 file looks like this:

Wondering if anyone can tell me my this is happening, and what a solution might be. For reference, I am using Matlab R2016a (9.0.0.341360) operating on Windows 10.
Thanks!
Accepted Answer
More Answers (0)
Categories
Find more on HDF5 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!