Matlab和HDF5:现在将可变长度元素写入现有数据集

时间:2016-08-24 15:40:53

标签: matlab hdf5

我的目标是在HDF5中编写matlab单元格。我想在创建文件时只写出我的单元格的一个子集,稍后再编写其余部分。不幸的是,Matlab的高级HDF5功能无法处理单元格。因此我正在使用低级函数。

到目前为止,我能够创建文件,数据集并编写单元格的子集。但我无法重新打开该文件来编写另一个子集。

以下是创建文件并写入两个子集的代码:

% create a dummy cell
mycell{1,1} = [1:4];
mycell{2,1} = [2:6]; 
mycell{3,1} = [1:10]; 
mycell{4,1} = [1:3];
dim = size(mycell);  

% create the HDF5 file
file = H5F.create('myfile.h5','H5F_ACC_TRUNC','H5P_DEFAULT','H5P_DEFAULT');
filetype = H5T.vlen_create('H5T_NATIVE_DOUBLE');
space = H5S.create_simple(2,fliplr(dim),[]);
dcpl = H5P.create ('H5P_DATASET_CREATE');
dset = H5D.create(file,dsetName,filetype,space,dcpl);

%write the first subset (from 1 to 2)
memtype = H5T.vlen_create('H5T_NATIVE_DOUBLE');
start  = [1 1];
count  = [2 1];
H5S.select_hyperslab(space,'H5S_SELECT_SET',fliplr(start-1),[1 1],fliplr(count),[1,1]);
H5D.write (dset,memtype,'H5S_ALL',space,'H5P_DEFAULT', mycell);

%write the second subset (from 3 to 3, hence one element)
start  = [3 1];
count  = [1 1];
H5S.select_hyperslab(space,'H5S_SELECT_SET',fliplr(start-1),[1 1],fliplr(count),[1,1]);
H5D.write (dset,memtype,'H5S_ALL',space,'H5P_DEFAULT', mycell);

% close ressources
H5D.close(dset);
H5S.close(space);
H5T.close(filetype);
H5T.close(memtype);
H5F.close(file);

现在我想写现有文件中的最后一个子集:

% some data
mycell{1,1} = [1:4];
mycell{2,1} = [2:6]; 
mycell{3,1} = [1:10]; 
mycell{4,1} = [1:3];

% open the file
file = H5F.open('myfile.h5','H5F_ACC_RDWR','H5P_DEFAULT');
dset = H5D.open(file,'foo');
space = H5D.get_space(dset);
memtype = H5T.vlen_create('H5T_NATIVE_DOUBLE');
start  = [4 1];
count  = [1 1];
H5S.select_hyperslab(space,'H5S_SELECT_SET',fliplr(start), [1,1],fliplr(count),[1,1]);

% write the data
H5D.write(dset,memtype,'H5S_ALL',space,'H5P_DEFAULT',mycell);

不幸的是,最后一行失败并出现以下错误:

Error using hdf5lib2
The memory space selection is invalid.

Error in H5D.write (line 71)
H5ML.hdf5lib2('H5Dwrite', varargin{:});

知道如何解决内存空间问题吗?

谢谢, Sylv

0 个答案:

没有答案