在Matlab中写一个复合数组到h5?

时间:2018-01-17 13:26:34

标签: matlab hdf5

我需要写一个结构数组,如下面的h5?这该怎么做?非常感谢!

wdata.serial_no   = int32(5);
wdata.temperature = 6.0;
wdata.pressure    = 7.0;

wdata_array{1} = wdata;
wdata_array{2} = wdata;

我的试用版:

% construct the struct array
wdata.serial_no   = int32(5);
wdata.temperature = 6.0;
wdata.pressure    = 7.0;

wdata_array{1} = wdata;
wdata_array{2} = wdata; 

% open the file
file = H5F.create ('test.h5', 'H5F_ACC_TRUNC',...
    'H5P_DEFAULT', 'H5P_DEFAULT');

% build the compound memtype
intType   =H5T.copy('H5T_NATIVE_INT');
sz(1)     =H5T.get_size(intType);

doubleType=H5T.copy('H5T_NATIVE_DOUBLE');
sz(2)     =H5T.get_size(doubleType);

sz(3)     =H5T.get_size(doubleType);

offset(1)=0;
offset(2:3)=cumsum(sz(1:2));

memtype = H5T.create ('H5T_COMPOUND', sum(sz));
H5T.insert (memtype,...
    'serial_no', offset(1), intType);
H5T.insert (memtype,...
    'temperature', offset(2), doubleType);
H5T.insert (memtype,...
    'pressure', offset(3), doubleType);

ts_memtype = H5T.array_create(memtype, 2);

space = H5S.create_simple (1, 1, []);

dset = H5D.create (file, 'DS1', ts_memtype, space, 'H5P_DEFAULT');
H5D.write (dset, ts_memtype, 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT', wdata_array);

H5D.close (dset);
H5S.close (space);
H5F.close (file);

错误: 输入结构字段的数量(0)与在中定义的H5T_COMPOUND成员的数量不匹配 file(3)。

1 个答案:

答案 0 :(得分:-1)

我使用higher level Matlab hdf5 commands获得了更好的运气:

使用函数创建HDF5的示例

function struct2HDF5(inStruct,hdf5Name)
sFields = fieldnames(inStruct);
for sf = sFields.'    
    h5create(hdf5Name,['/' char(sf)], size(inStruct),'Datatype',class(inStruct(1).(char(sf))))
    h5write(hdf5Name,['/' char(sf)],[inStruct.(char(sf))])
end

>> wdata.serial_no   = int32(5);
>> wdata.temperature = 6.0;
>> wdata.pressure    = 7.0;

>> wdata_array(1) = wdata;
>> wdata_array(2) = wdata;

>> struct2HDF5(wdata_array,'test3.h5')
>> h5disp('test3.h5')
HDF5 test3.h5 
Group '/' 
    Dataset 'pressure' 
        Size:  1x2
        MaxSize:  1x2
        Datatype:   H5T_IEEE_F64LE (double)
        ChunkSize:  []
        Filters:  none
        FillValue:  0.000000
    Dataset 'serial_no' 
        Size:  1x2
        MaxSize:  1x2
        Datatype:   H5T_STD_I32LE (int32)
        ChunkSize:  []
        Filters:  none
        FillValue:  0
    Dataset 'temperature' 
        Size:  1x2
        MaxSize:  1x2
        Datatype:   H5T_IEEE_F64LE (double)
        ChunkSize:  []
        Filters:  none
        FillValue:  0.000000
>> press = h5read('test3.h5','/pressure')    
press =    
     7     7

请注意,当您获取数据时,它将位于数组中。另外,您可能需要注意结构中的某些内容是HDF5不支持的类型,如单元格或字符数组。

编辑:
这是从HDF5制作结构数组的反向函数。请注意,有一些假设,比如每个字段都是相同的维度等。

function outStruct = HDF52struct(hdf5Name)
fInfo = h5info(hdf5Name);
sFields ={fInfo.Datasets.Name};
sSize   = max(fInfo.Datasets(1).Dataspace.Size);
outStruct(sSize).(sFields{1}) = []; %Create struct array with the right number of elements
for sf = sFields 
    tempData = num2cell(h5read(hdf5Name,['/' char(sf)]));
    [outStruct.(char(sf))] = deal(tempData{:});
end