Can't write H5T_STD_U32LE instead it automaticallly converts to H5T_STD_U8LE

조회 수: 3 (최근 30일)
Joshua Ludwig
Joshua Ludwig 2022년 1월 19일
답변: Kartik Saxena 2024년 1월 17일
I am trying to write an attribute to an HDF5 file, the program that will read it expects:
DATATYPE H5T_STD_U32LE
DATASPACE SCALAR
When I use Matlab's h5writeatt I get:
DATATYPE H5T_STD_U32LE
DATASPACE SIMPLE { ( 1 ) / ( 1 ) }
When I use the following code I get the warning "Conversion from uint32 to datatype H5T_STD_U8LE may clamp some values. "
and h5dump shows that this was written as a H5T_STD_U8LE, despite me specifically requesting H5T_STD_U32LE.
dlength=1;
dvals=1;
attname='/openPMDextension/';
attloc='/';
fname='\closedpmd_00000.h5';
fid = H5F.open(fname,'H5F_ACC_RDWR','H5P_DEFAULT');
acpl = H5P.create('H5P_ATTRIBUTE_CREATE');
type_id = H5T.copy('H5T_STD_U32LE');
H5T.set_size(type_id,dlength)
space_id = H5S.create('H5S_SCALAR');
attr_id = H5A.create(fid,attname,type_id,space_id,acpl);
H5A.write(attr_id,'H5ML_DEFAULT',dvals)
H5A.close(attr_id);
H5F.close(fid);
H5T.close(type_id);

답변 (1개)

Kartik Saxena
Kartik Saxena 2024년 1월 17일
Hi,
From the warning "Conversion from uint32 to datatype H5T_STD_U8LE may clamp some values", I understand that the data you are trying to write is being automatically converted to a different datatype. This is likely because the data you are passing to 'H5A.write' is of type 'uint32', but the attribute's datatype is set to 'H5T_STD_U32LE', which is a 32-bit unsigned integer.
To resolve this issue, you can explicitly convert your data to the desired datatype before writing it to the attribute. Here's an example of how you can modify your code:
dlength = 1;
dvals = uint32(1); % Convert data to uint32
attname = '/openPMDextension/';
attloc = '/';
fname = '\closedpmd_00000.h5';
fid = H5F.open(fname, 'H5F_ACC_RDWR', 'H5P_DEFAULT');
acpl = H5P.create('H5P_ATTRIBUTE_CREATE');
type_id = H5T.copy('H5T_STD_U32LE');
H5T.set_size(type_id, dlength);
space_id = H5S.create('H5S_SCALAR');
attr_id = H5A.create(fid, attname, type_id, space_id, acpl);
H5A.write(attr_id, 'H5ML_DEFAULT', dvals);
H5A.close(attr_id);
H5F.close(fid);
H5T.close(type_id);
By explicitly converting 'dvals' to 'uint32', you ensure that the data is written as a 32-bit unsigned integer, as specified by 'H5T_STD_U32LE'.
I hope it helps!
If it doesn't, let me know and I'll be happy to look further into this.

카테고리

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

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by