필터 지우기
필터 지우기

How to create a HDF5 group with only attributes

조회 수: 5 (최근 30일)
HaMo
HaMo 2023년 9월 7일
답변: Manish 2024년 10월 4일 12:03
Writing an attribute to a group that already has a dataset works fine:
h5create("sample.hdf5", "/group/dataset", 1:3)
h5writeatt("sample.hdf5", "/group/", "attribute", 3.14)
But I cannot create attributes in files that do not alredy exist:
h5create("sample2.hdf5", "/group1/dataset", 1:3)
h5writeatt("sample2.hdf5", "/group2/", "attribute", 3.14)
Error using hdf5lib2
The HDF5 library encountered an error and produced the following stack trace information:
H5G_loc_find_cb object 'group2' doesn't exist
(...)
There is no "create attribute" function as there is for datasets. I understand this constraint may be there for a purpose, but is there a workaround?

답변 (1개)

Manish
Manish 2024년 10월 4일 12:03
Hi Hamo,I encountered a similar issue and found a workaround.
Before writing the attribute, explicitly create the group by opening the file.
Here is the code implementation:
h5create("sample.hdf5", "/group/dataset", [1 3]);
h5writeatt("sample.hdf5", "/group/", "attribute", 3.14);
h5create("sample2.hdf5", "/group1/dataset", [1 3]);
% Workaround:
fileID = H5F.open('sample2.hdf5', 'H5F_ACC_RDWR', 'H5P_DEFAULT');
groupID = H5G.create(fileID, '/group2', 'H5P_DEFAULT', 'H5P_DEFAULT', 'H5P_DEFAULT');
H5G.close(groupID);
H5F.close(fileID);
% Now write the attribute to the newly created group
h5writeatt("sample2.hdf5", "/group2/", "attribute",3.14);
It solved for me, Hope this solves for you!

카테고리

Help CenterFile Exchange에서 HDF5에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by