Write Strings to Existing HDF5 file

21 vues (au cours des 30 derniers jours)
Keith Marcoe
Keith Marcoe le 27 Juin 2017
Commenté : Walter Roberson le 28 Juin 2017
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!

Réponse acceptée

Walter Roberson
Walter Roberson le 27 Juin 2017
Try
H5D.write (dataset_id, memtype, 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT', wdata .');
  2 commentaires
Keith Marcoe
Keith Marcoe le 27 Juin 2017
That worked, thank you.
Can you provide a brief explanation as to why? I'm still getting familiar w/ Matlab.
Thanks again..
Walter Roberson
Walter Roberson le 28 Juin 2017
The assignment you used created a 4 x something character array. In MATLAB, the memory arrangement is down the columns, so the next item in memory after X(I,J) is X(I+1,J) rather than X(I,J+1) . So as data is filled from the char array into the HDF5 arrays, it follows the "down the columns" arrangement.
The same effect happens when you go to sprintf() or fprintf() arrays:
fprintf('%d %d\n', [1 2; 3 4; 5 6])
is going to give
1 3
5 2
4 6
as the output because it fills by consecutive memory locations. So if you have
fprintf( some_format, YourArray )
chances are you are going to need to use
fprintf( some_format, YourArray .')
instead to get the rows you expect to see.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by