Writing Cell Attributes to HDF5 File
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
In Matlab R2018a, I can write HDF5 attributes with a cell input. Following the Matlab example for using hdf5write:
attr = {'Time' 'Var1' 'Var2'};
attr_details.Name = 'ColumnHeaders';
attr_details.AttachedTo = '/group';
attr_details.AttachType = 'dataset';
dset = rand(10,3);
dset_details.Location = '/';
dset_details.Name = 'group';
hdf5write('myfile.h5', dset_details, dset, attr_details, attr);
I noticed that Matlab is now warning that hdf5write will be removed, and says to use h5write instead. However, I can't seem to write attributes with a cell array. When I try to use h5writeatt with the attr variable above, I get an error that says "'cell' is not supported as an attribute datatype."
h5create('myfile.h5', '/group', size(dset));
h5write('myfile.h5', '/group', dset);
h5writeatt('myfile.h5', '/group', 'ColumnHeaders', attr);
% h5writeatt('myfile.h5', '/group', 'ColumnHeaders', attr, 'TextEncoding', 'system'); % give same error
Is there a workaround to this, or will it be an added feature whenever hdf5write is removed?
0 commentaires
Réponses (1)
Deepak
le 6 Jan 2025
In MATLAB, the transition from hdf5write to h5write and h5writeatt indeed comes with some changes in how data types are handled, particularly with attributes. The h5writeatt function does not directly support cell arrays as attribute data types, which is why getting an error. However, we can work around this by converting the cell array into a format that is supported, such as a character array.
Below is the sample MATLAB code to achieve the same:
% Create the dataset
dset = rand(10,3);
h5create('myfile.h5', '/group', size(dset));
h5write('myfile.h5', '/group', dset);
% Convert the cell array to a character array
attr = {'Time', 'Var1', 'Var2'};
attr_str = strjoin(attr, ', '); % Join with a delimiter, e.g., comma and space
% Write the attribute as a character array
h5writeatt('myfile.h5', '/group', 'ColumnHeaders', attr_str);
Please find attached the documentation of functions used for reference:
I hope this helps in resolving the issue.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!