How to save struct array into H5

조회 수: 37 (최근 30일)
Kat Lee
Kat Lee 2022년 4월 8일
답변: Shivam Lahoti 2023년 11월 16일
Hi guys
I have this simple struct array wdata, has 4 fileds and saved as struct array
FieldA =int32([1153 ; 1184 ; 1027;11 ]);
FieldB ={'Exterior (static)', 'Intake',...
'Intake manifold','test'};
FieldArray = [randn(1,ADIM0);randn(1,ADIM0);randn(1,ADIM0);randn(1,ADIM0)];
FieldC =int32([11 ; 12 ; 13; 14]);
for i = 1:DIM0
wdata(i).FieldA =FieldA(i);
wdata(i).FieldB =FieldB(i);
wdata(i).FieldArray = FieldArray(i);
wdata(i).FieldC = FieldC;
end
I want to save wdata into h5 as compound, but I got stuck with memory assigning
can anyone help?
thansk
  댓글 수: 1
Kat Lee
Kat Lee 2022년 4월 8일
편집: Kat Lee 2022년 4월 8일
Tried this, but failed
dim = 4;
file = H5F.create (fileName, 'H5F_ACC_TRUNC',...
'H5P_DEFAULT', 'H5P_DEFAULT');
intType =H5T.copy('H5T_NATIVE_INT');
sz(1) = H5T.get_size(intType);
strType = H5T.copy ('H5T_C_S1');
H5T.set_size (strType, 'H5T_VARIABLE');
sz(2) =H5T.get_size(strType);
% array size
base_type_id = H5T.copy('H5T_NATIVE_DOUBLE');
arrayType = H5T.array_create(base_type_id, 1, ADIM0);
type_id = H5T.copy(arrayType);
H5T.set_size(type_id, 8);
sz(3) = H5T.get_size(arrayType);
intType =H5T.copy('H5T_NATIVE_INT');
sz(4) = H5T.get_size(intType);
offset(1)=0;
offset(2:4)=cumsum(sz(1:3));
memtype = H5T.create ('H5T_COMPOUND', sum(sz));
H5T.insert (memtype,...
'FieldA',offset(1),intType);
H5T.insert (memtype,...
'FieldB',offset(2), strType);
H5T.insert (memtype,...
'FieldArray',offset(3), arrayType);
H5T.insert (memtype,...
'FieldC',offset(4), intType);
filetype = H5T.create ('H5T_COMPOUND', sum(sz));
H5T.insert (filetype, 'FieldA', offset(1),intType);
H5T.insert (filetype, 'FieldB', offset(2), strType);
H5T.insert (filetype, 'FieldArray',offset(3), arrayType);
H5T.insert (filetype, 'FieldC',offset(4), intType);
space = H5S.create_simple (1,fliplr(dims), []);
dset = H5D.create (file, DATASET, filetype, space, 'H5P_DEFAULT');
H5D.write (dset, memtype, 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT', flip(wdata));

댓글을 달려면 로그인하십시오.

답변 (1개)

Shivam Lahoti
Shivam Lahoti 2023년 11월 16일
Hi Kat Lee,
I can understand that you want to save the wdata struct array into an HDF5 file as a compound datatype. However, you mentioned that you encountered issues with memory assignment.
You shall use the functions h5create’ to create the HDF5 file and define the compound datatype and ‘h5write’ to write each struct element to the HDF5 file.
Execute this command into your MATLAB window to understand the implementation of both functions with the help of an example.
openExample('matlab/WriteToEntireDataSetExample')
Refer to the documentation below for more clarity.
I hope this was helpful.
Regards,
Shivam Lahoti.

Community Treasure Hunt

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

Start Hunting!

Translated by