matlab - 通过MATLAB向HDF5文件中Datatype的每个成员写入矩阵数据

标签 matlab dataset hdf5

这是我第一次尝试使用 Low-Level commands 从头开始​​创建 HDF5 文件通过 MATLAB。

我的问题是,我很难尝试将数据写入数据集数据类型中的每个特定成员。

首先,我创建一个新的 HDF5 文件,并设置正确的组层:

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');

然后,我将其格式化为我的数据集:

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);

Error using hdf5lib2

Unhandled HDF5 class (H5T_NO_CLASS) encountered. It is not possible to write to this attribute or dataset.

所以,我改用这个方法:

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);

Error using hdf5lib2

Attempted to transfer too many values to or from the library buffer.

看的时候here对于错误消息的 XML 标记,它将上述错误描述为“illegalArrayAccess”。显然,根据 this question ,您只能写入 4 个成员而缓冲区不会抛出错误?

这是正确的吗?我怎样才能正确地写给每个成员。我即将达到我的精神极限试图解决这个问题。

编辑:

此处保留的引用资料用于提供一般信息:

HDF5 Compound Datatypes Example

HDF5 Compount Datatypes

H5D.write MATLAB Command

最佳答案

我发现为什么我不能写数据了。我已经解决了这个问题。我的尺寸设置不正确(这是我最初忘记包含的代码)。我很抱歉。我的尺寸是这样的:

dims = fliplr(size(data_matrix));

其中 dims 是一个 15x250 矩阵。错误在于缓冲区无法为每个成员写入 250x15 矩阵,因为它只有每个成员的 250x1 数据。

以下代码将(通常)用于将数据写入每个成员:

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。

关于matlab - 通过MATLAB向HDF5文件中Datatype的每个成员写入矩阵数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55539831/

相关文章:

java - Matlab 2014b javax 错误与 ubuntu 中的绘图

matlab - 通过与另一个数组进行比较来查找数组中的元素

c - 在 C 中读取 Matlab .mat 文件

javascript - 在 jQuery 中创建元素时如何使用数据集属性?

xml - 如何插入换行符dbunit数据集

c# - GetXml方法时DataSet是否支持复杂类型?

python : Dot product of dask array

matlab - 如何绘制实心圆?

python - pandas.HDFStore : How do I modify "data_columns" for an existing store? 我想向不在数据列中的列添加索引

python - 在同一 Python 进程中同时使用 h5py 和 pytables