通过MATLAB将矩阵数据写入HDF5文件中的每个数据类型成员 [英] Write Matrix Data to Each Member of Datatype in HDF5 file via MATLAB
问题描述
这是我尝试使用我的问题是我很难将数据写入数据集中数据类型的每个特定成员.
My issue is that I am having a hard time trying to write data to each specific member in the datatype on my dataset.
首先,我创建一个新的HDF5文件,并设置正确的组图层:
First, I create a new HDF5 file, and set the right layer of groups:
new_h5 = H5F.create('new_hdf5_file.h5','H5F_ACC_TRUNC','H5P_DEFAULT','H5P_DEFAULT');
new_h5 = H5G.create(new_h5,'first','H5P_DEFAULT','H5P_DEFAULT','H5P_DEFAULT');
new_h5 = H5G.create(new_h5,'second','H5P_DEFAULT','H5P_DEFAULT','H5P_DEFAULT');
然后,我创建我的数据类型:
Then, I create my datatype:
datatype = H5T.create('H5T_compound',20);
H5T.insert(datatype,'first_element',0,'H5T_NATIVE_INT');
H5T.insert(datatype,'second_element',4,'H5T_NATIVE_DOUBLE');
H5T.insert(datatype,'third_element',12,'H5T_NATIVE_DOUBLE');
然后,将其格式化为我的数据集:
Then, I format that into my dataset:
new_h5 = H5D.create(new_h5,'location',datatype,H5S.create('H5S_SCALAR'),'H5P_DEFAULT');
subset = H5D.get_type(H5D.open(new_h5,'/first/second/location'));
mem_type = H5T.get_member_type(subset,0);
我收到以下命令错误:
H5D.write(mem_type,'H5ML_DEFAULT','H5S_ALL','H5S_ALL','H5P_DEFAULT',data);
使用hdf5lib2时出错
Error using hdf5lib2
遇到未处理的HDF5类(H5T_NO_CLASS).无法写入该属性或数据集.
Unhandled HDF5 class (H5T_NO_CLASS) encountered. It is not possible to write to this attribute or dataset.
所以,我改用这种方法:
So, I try this method instead:
new_h5 = H5D.create(new_h5,'location',datatype,H5S.create_simple(2,dims,dims),'H5P_DEFAULT'); %where dims are the dimensions of all matrices of data structure
H5D.write(mem_type,'H5ML_DEFAULT','H5S_ALL','H5S_ALL','H5P_DEFAULT',data); %where data is a structure
我收到以下命令错误:
H5D.write(mem_type,'H5ML_DEFAULT','H5S_ALL','H5S_ALL','H5P_DEFAULT',data);
使用hdf5lib2时出错
Error using hdf5lib2
试图向库缓冲区或从库缓冲区传输太多值.
Attempted to transfer too many values to or from the library buffer.
在此处时,对于错误消息的XML标签,它将上述错误描述为"illegalArrayAccess".显然,根据
When looking here for the XML tags for the error messages, it describes the above error as "illegalArrayAccess." Apparently, according to this question, you can only write to 4 members without the buffer throwing an error?
这是正确的吗?如何正确写信给每个成员.我将尽力解决这个问题.
Is this correct? How can I correctly write to each member. I am about to reach my mental limit trying to figure this one out.
EDIT :
EDIT:
此处保留参考,以获取一般信息:
References kept here for general information:
推荐答案
我发现了为什么我不能写数据的原因.我已经解决了问题.我的尺寸设置不正确(这是我最初忘记包含的代码).我很抱歉.我的尺寸是这样的:
I found out why I cannot write data. I have solved the problem. I had my dimensions set incorrectly (which is code I forgot to include originally). My apologies. I had my dimensions like this:
dims = fliplr(size(data_matrix));
暗处是15x250的矩阵.错误在于缓冲区无法为每个成员写入250x15的矩阵,因为每个成员只有250x1的数据.
Where dims was a 15x250 matrix. The error was in that the buffer was unable to write a 250x15 matrix for each member, because it only had data for a 250x1 for each member.
以下代码(通常)可用于将数据写入每个成员:
The following code will (generically) work for writing data to each member:
new_h5 = H5F.create('new_hdf5_file.h5','H5F_ACC_TRUNC','H5P_DEFAULT','H5P_DEFAULT');
new_h5 = H5G.create(new_h5,'first','H5P_DEFAULT','H5P_DEFAULT','H5P_DEFAULT');
new_h5 = H5G.create(new_h5,'second','H5P_DEFAULT','H5P_DEFAULT','H5P_DEFAULT');
datatype = H5T.create('H5T_compound',20);
H5T.insert(datatype,'first_element',0,'H5T_NATIVE_INT');
H5T.insert(datatype,'second_element',4,'H5T_NATIVE_DOUBLE');
H5T.insert(datatype,'third_element',12,'H5T_NATIVE_DOUBLE');
dims = fliplr(size(data_matrix)); dims = [1 dims(1,2)];
new_h5 = H5D.create(new_h5,'location',datatype,H5S.create_simple(2,dims,dims),'H5P_DEFAULT');
H5D.write(new_h5,'H5ML_DEFAULT','H5S_ALL','H5S_ALL','H5P_DEFAULT',data_structure);
其中data_matrix
是一个包含所有数据的15x250矩阵,而data_structure
是一个包含15个字段的结构,每个字段的大小为250x1.
where data_matrix
is a 15x250 matrix containing all data, and where data_structure
is a sctucture containing 15 fields, each 250x1 in size.
这篇关于通过MATLAB将矩阵数据写入HDF5文件中的每个数据类型成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!