Python:使用h5py和NumPy从MATLAB.mat文件中读取str
我很难将MATLAB.mat文件中的'str'变量'Et'(Endtime)和'St'(Starttime)加载到Python中 我想要与MATLAB中相同的输出。相反,我在试图解决这个问题时遇到了一些问题。请参见下面的Python代码和输出Python:使用h5py和NumPy从MATLAB.mat文件中读取str,python,string,matlab,numpy,h5py,Python,String,Matlab,Numpy,H5py,我很难将MATLAB.mat文件中的'str'变量'Et'(Endtime)和'St'(Starttime)加载到Python中 我想要与MATLAB中相同的输出。相反,我在试图解决这个问题时遇到了一些问题。请参见下面的Python代码和输出 # Import numpy and h5py to load in .mat files import numpy as np import h5py # Load in Matlab ('-v7.3') data fname = 'director
# Import numpy and h5py to load in .mat files
import numpy as np
import h5py
# Load in Matlab ('-v7.3') data
fname = 'directory/file.mat'
f = h5py.File(fname,'r')
# create dictionary for data
data= {"average":np.array(f.get('average')),"median":np.array(f.get('median')), \
"stdev":np.array(f.get('stdev')),"P10":np.array(f.get('p10')), \
"P90":np.array(f.get('p90')),"St":np.str(f.get('stime')), \
"Et":np.str(f.get('etime'))}
# All other variables are arrays
print(data["Et"])
输出:
<HDF5 dataset "etime": shape (1, 6), type "<u4">
我想让python中的字符串与MATLAB中的字符串相等。
换句话说,我想要打印(data[“Et”])='01011212000000',这是日期和时间
我怎样才能解决这个问题
MATLAB中的数据示例:
当我需要加载
.mat
时,我使用scipy
,它工作正常。
试试这个:
import scipy.io
mat = scipy.io.loadmat('fileName.mat')
我想它会起作用的。
祝你好运。八度音阶
>> x = 1:10;
>> y = reshape(1:12, 3,4);
>> et = '0101121200000';
>> xt = 'a string';
>> save -hdf5 testh5.mat x y et xt
在numpy会议中:
In [130]: f = h5py.File('testh5.mat','r')
In [131]: list(f.keys())
Out[131]: ['et', 'x', 'xt', 'y']
In [132]: list(f['y'].keys())
Out[132]: ['type', 'value']
In [133]: f['x/type'].value
Out[133]: b'range'
In [134]: f['y/type'].value
Out[134]: b'matrix'
In [135]: f['y/value'].value
Out[135]:
array([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.],
[10., 11., 12.]])
In [136]: f['et/type'].value
Out[136]: b'sq_string'
In [137]: f['et/value'].value
Out[137]:
array([[48],
[49],
[48],
[49],
[49],
[50],
[49],
[50],
[48],
[48],
[48],
[48],
[48]], dtype=int8)
In [138]: f['et/value'].value.ravel().view('S13')
Out[138]: array([b'0101121200000'], dtype='|S13')
In [139]: f['xt/value'].value.ravel().view('S8')
Out[139]: array([b'a string'], dtype='|S8')
In [140]: f.close()
====
字节
也适用于我的文件
In [220]: bytes(f['xt/value'].value)
Out[220]: b'a string'
In [221]: bytes(f['et/value'].value)
Out[221]: b'0101121200000'
如果您不介意
file.mat
中存储的etime
和stime
的变量类型,并且您可以将它们存储为typechar
而不是string
,您可以通过以下方式在Python中读取它们:字节(f.get(您的变量).value).decode('utf-8')
。就你而言:
data = {
"average": np.array(f.get('average')),
"median": np.array(f.get('median')),
"stdev": np.array(f.get('stdev')),
"P10": np.array(f.get('p10')),
"P90": np.array(f.get('p90')),
"St": bytes(f.get('stime')[:]).decode('utf-8'),
"Et": bytes(f.get('etime')[:]).decode('utf-8')
}
我确信还有一种方法可以读取
字符串
类型,但这可能是最简单的解决方案。听起来好像OP用较新的hdf5模式保存了.mat,而不是兼容的loadmat
模式。执行此过程时,我看不到任何字符串变量。输出:dict_键(['''''''''''''''''''''''''''''''''''''''''u版本'''''''''.'全局'''.'平均','stdev','median'.'P90','P10','None'.''''.'''''''.'''''.'''''''.'''''''''.'函数'.'工作区'.'.'.'.'.'无Et或St.'注:不必担心NAN-它们应该是这样的。至少在八度'hdf5'文件中,f[/code>f。把两者分开阅读是个好主意。对于字符串,type
是b'sq_string'
,value
是一个“int8”数据类型的(n,1)数组。我认为,这可以通过testring转换为Python。在加载hdf5
mat文件的过程中,出现了一些这样的问题,尽管我不记得是否有人看过字符串,f.get('etime')
?它是一个组还是一个数据集?如果是一个组,它是否有任何键?请尝试np.array(f.get('etime'))
。将其作为数组加载;之后我们可能可以“解码”它,就像我在[138]中的中所做的那样。让我们细化np.array(f.get('etime'),dtype='这对我不起作用。当使用list(f['average'].keys())时,
我得到以下错误:AttributeError:'Dataset'对象没有属性'keys'。好的,在我的版本中,f['average']
是一个包含2个数据集的组。显然在您的f['average']
是数据集本身。我没有你的文件,所以无法亲自查看。“average”是一个9 x 365的矩阵,其中大部分是NaN,到处都有一些浮点数。仔细研究,我发现MATLAB v7.3和Octave的hdf5之间有更大的差异。如果没有示例文件,我就无能为力。@hpualj我在MATLAB.I c中添加了数据的图像我们找不到附加.mat文件的方法谢谢@machnic!这非常有效。我现在已经将字符串从.mat文件加载到python中。