Writing and modifying attributes to h5 files
46 views (last 30 days)
Show older comments
I have a *.h5 file with two groups - '/info' and '/data'. The '/info' group contains a series of 14 attributes that provide relevant information about the datasets in '/data'. I want to develop an 'attribute editor' for the '/info' attributes, but I am having difficultly modifying the existing attributes. I have tried two approaches using hdf5write and h5attput (from hdf5tools on matlab central).
Using hdf5write I can do the following:
attr = ‘new_info';
attr_details.Name = 'New_Attribute';
attr_details.AttachedTo = '/info’;
attr_details.AttachType = 'group';
hdf5write('myh5file.h5',attr_details, attr, 'WriteMode', 'append');
If the attribute didn’t exist previously, this seems to work fine – I get a new attribute ‘/info/New_Attribute’ whose Value.Data = ’new_info’. However, if I decide that I want to change this by continuing with
attr = ‘newer_info';
hdf5write('myh5file.h5',attr_details, attr, 'WriteMode', 'append');
I get the error
??? Error using ==> hdf5writec
writeH5Attr: Couldn't create attribute.
Error in ==> hdf5write at 143
hdf5writec(file, details, varargin{dset_idx});
Changing the ‘WriteMode’ to ‘overwrite’ doesn’t work either.
Using h5attput is similar, as I can create a new attribute with the command
h5attput('myh5file.h5','/info','New_Attribute','new_info’);
but trying to modify ‘New_Attribute’ gives an error, i.e.
h5attput('myh5file.h5','/info','New_Attribute','newer_info’);
??? Error using ==> hdf5lib2
The HDF5 library encountered an error: "Unknown error."
Error in ==> write at 36
H5ML.hdf5lib2('H5Awrite', varargin{:});
Error in ==> h5attput at 51
H5A.write(att_id,datatype_id,attvalue);
The difference with h5attput is that for numeric data, I am able to modify the attributes, so for example:
h5attput('myh5file.h5','/info','Numeric_Attribute',12);
creates the attribute ‘/info/Numeric_Attribute’ with Value = 12, which can be updated successfully with something like:
h5attput('myh5file.h5','/info','Numeric_Attribute',58);
So, in summary, I can use hdf5write or h5attput to create new attributes within an existing group, but I cannot modify the attributes using commands similar to those used to create them. The exception is using h5attput in the case where the attribute has a numeric value. I have also tried creating an h5 string object
new_info = hdf5.h5string('new_info’)
but neither hdf5write or h5attput accepts the h5 string as an input.
Thanks in advance for any input and ideas.
0 Comments
Answers (1)
Ashish Uthama
on 17 Feb 2011
HDF5 attributes cannot be 'overwritten' with a different datatype.
Since both the functions you mention work for you in creating attributes, one approach would be to delete the attribute iff it already exists and then create a new one.
Here is a snippet showing how to delete an existing attribute:
h5file = 'myfile.h5';
location = '/info';
attributeName = 'Some_Attribute';
% Open the file (ensure to close it automatically when done)
fileID = H5F.open(h5file,'H5F_ACC_RDWR','H5P_DEFAULT');
fileIDCleanUp = onCleanup(@()H5F.close(fileID));
% Open the dataset/group
locID = H5O.open(fileID, location,'H5P_DEFAULT');
locIDCleanUp = onCleanup(@()H5O.close(locID));
try %to open the attribute.
attID = H5A.open(locID, attributeName, 'H5P_DEFAULT');
H5A.close(attID);
H5A.delete(locID, attributeName);
catch ALL
% do nothing if the attribute does not exist.
end
0 Comments
See Also
Categories
Find more on HDF5 in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!