是否可以使用package hdf5storage向一个hdf5storage文件(v7.3)添加一个新变量
示例:
我在Matlab中写道:
test = {'Hello', 'world!'; 'Good', 'morning'; 'See', 'you!'};
save('data.mat', 'test', '-v7.3') % v7.3 so that it is readable by h5py

在Python中,我想向data.mat添加一个新变量。我怎样才能做到这一点,例如:

我试过:
import hdf5storage # get code on https://pypi.python.org/pypi/hdf5storage/0.1.3
import numpy as np
matcontent = {}
matcontent[u'some_numbers'] = np.array([10, 50, 20]) # each key must be a unicode string
hdf5storage.write(matcontent, '.', 'data.mat', matlab_compatible=True)但是它覆盖了data.mat,而不是添加一个新变量。
发布于 2015-02-05 11:09:53
您正在创建新数据,然后将该新数据写入文件。它覆盖了文件。您需要加载原始.mat文件,并将其附加到其中,然后再次保存。
import hdf5storage
import numpy as np
matcontent = hdf5storage.loadmat('data.mat')
matcontent[u'some_numbers'] = np.array([10, 50, 20])
hdf5storage.savemat('data.mat', matcontent)然后在Matlab中
>> whos -file data.mat
Name Size Bytes Class Attributes
some_numbers 1x3 24 int64
test 3x2 730 cell 发布于 2015-11-25 10:42:12
到目前为止我知道这是不可能的。TheBlackCat提供的答案并不适用,因为您正在重写您的文件。我倾向于有很大的matlab文件,我通常不想完全阅读,而是有选择地读或写。这是在HDF5文件中使用的底层.mat格式的最大优势(连同引用)。Python包hdf5storage仍然在0.xx版本中,因此我认为这将在以后的版本中出现。
https://stackoverflow.com/questions/28265495
复制相似问题