在python中读取v 7.3 mat文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17316880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reading v 7.3 mat file in python
提问by Shan
I am trying to read a matlab file with the following code
我正在尝试使用以下代码读取 matlab 文件
import scipy.io
mat = scipy.io.loadmat('test.mat')
and it gives me the following error
它给了我以下错误
raise NotImplementedError('Please use HDF reader for matlab v7.3 files')
NotImplementedError: Please use HDF reader for matlab v7.3 files
so could anyone please had the same problem and could please any sample code
所以任何人都可以遇到同样的问题并且可以请任何示例代码
thanks
谢谢
回答by lee
According to the Scipy cookbook. http://wiki.scipy.org/Cookbook/Reading_mat_files,
根据 Scipy 食谱。http://wiki.scipy.org/Cookbook/Reading_mat_files,
Beginning at release 7.3 of Matlab, mat files are actually saved using the HDF5 format by default (except if you use the -vX flag at save time, see help save in Matlab). These files can be read in Python using, for instance, the PyTables or h5py package. Reading Matlab structures in mat files does not seem supported at this point.
从 Matlab 7.3 版开始,mat 文件实际上默认使用 HDF5 格式保存(除非您在保存时使用 -vX 标志,请参阅 Matlab 中的帮助保存)。例如,可以使用 PyTables 或 h5py 包在 Python 中读取这些文件。目前似乎不支持在 mat 文件中读取 Matlab 结构。
Perhaps you could use Octave to re-save using the -vX flag.
也许您可以使用 Octave 使用 -vX 标志重新保存。
回答by norok2
import h5py
import numpy as np
filepath = '/path/to/data.mat'
arrays = {}
f = h5py.File(filepath)
for k, v in f.items():
arrays[k] = np.array(v)
you should end up with your data in the arrays
dict, unless you have MATLAB structures, I suspect. Hope it helps!
你应该在arrays
字典中得到你的数据,除非你有 MATLAB 结构,我怀疑。希望能帮助到你!
回答by Léonard
I had a look at this issue: https://github.com/h5py/h5py/issues/726. If you saved your mat file with -v7.3
option, you should generate the list of keys with (under Python 3.x):
我看了一下这个问题:https: //github.com/h5py/h5py/issues/726。如果您使用-v7.3
选项保存了 mat 文件,您应该使用(在 Python 3.x 下)生成密钥列表:
import h5py
with h5py.File('test.mat', 'r') as file:
print(list(file.keys()))
In order to access the variable a
for instance, you have to use the same trick:
a
例如,为了访问变量,您必须使用相同的技巧:
with h5py.File('test.mat', 'r') as file:
a = list(file['a'])
回答by Maxim
Per Magu_'s answer on a related thread, check out the package hdf5storagewhich has convenience functions to read v7.3 matlab mat files; it is as simple as
根据Magu_ 在相关线程上的回答,查看包hdf5storage,它具有读取 v7.3 matlab mat 文件的便利功能;就这么简单
import hdf5storage
mat = hdf5storage.loadmat('test.mat')
回答by Stephen Morrell
Despite hours of searching I've not found how to access Matlab v7.3 structures either. Hopefully this partial answer will help someone, and I'd be very happy to see extra pointers.
尽管搜索了数小时,我也没有找到如何访问 Matlab v7.3 结构。希望这个部分答案会对某人有所帮助,我很高兴看到额外的指示。
So starting with (I think the [0][0] arises from Matlab giving everything to dimensions):
所以从(我认为 [0][0] 源于 Matlab 将所有内容都赋予维度):
f = h5py.File('filename', 'r')
f['varname'][0][0]
gives: < HDF5 object reference >
给出:<HDF5 对象引用>
Pass this reference to f again:
再次将此引用传递给 f:
f[f['varname'][0][0]]
which gives an array: convert this to a numpy array and extract the value (or, recursively, another < HDF5 object reference > :
它给出了一个数组:将其转换为一个 numpy 数组并提取值(或者,递归地,另一个 < HDF5 对象引用 > :
np.array(f[f['varname'][0][0]])[0][0]
If accessing the disk is slow, maybe loading to memory would help.
如果访问磁盘很慢,也许加载到内存会有所帮助。
Further edit: after much futile searching my final workaround (I really hope someone else has a better solution!) was calling Matlab from python which is pretty easy and fast:
进一步编辑:经过多次徒劳的搜索,我的最终解决方法(我真的希望其他人有更好的解决方案!)从 python 调用 Matlab,这非常简单快捷:
eng = matlab.engine.start_matlab() # first fire up a Matlab instance
eng.quit()
eng = matlab.engine.connect_matlab() # or connect to an existing one
eng.sqrt(4.0)
x = 4.0
eng.workspace['y'] = x
a = eng.eval('sqrt(y)')
print(a)
x = eng.eval('parameterised_function_in_Matlab(1, 1)', nargout=1)
a = eng.eval('Structured_variable{1}{2}.object_name') # (nested cell, cell, object)
回答by L. K?rkk?inen
This function reads Matlab-produced HDF5 .mat files, and returns a structure of nested dicts of Numpy arrays. Matlab writes matrices in Fortran order, so this also transposes matrices and higher-dimensional arrays into conventional Numpy order arr[..., page, row, col]
.
此函数读取 Matlab 生成的 HDF5 .mat 文件,并返回 Numpy 数组的嵌套字典结构。Matlab 以 Fortran 顺序写入矩阵,因此这也将矩阵和高维数组转置为传统的 Numpy 顺序arr[..., page, row, col]
。
import h5py
def read_matlab(filename):
def conv(path=''):
p = path or '/'
paths[p] = ret = {}
for k, v in f[p].items():
if type(v).__name__ == 'Group':
ret[k] = conv(f'{path}/{k}') # Nested struct
continue
v = v[()] # It's a Numpy array now
if v.dtype == 'object':
# HDF5ObjectReferences are converted into a list of actual pointers
ret[k] = [r and paths.get(f[r].name, f[r].name) for r in v.flat]
else:
# Matrices and other numeric arrays
ret[k] = v if v.ndim < 2 else v.swapaxes(-1, -2)
return ret
paths = {}
with h5py.File(filename, 'r') as f:
return conv()
回答by skjerns
I've created a small libraryto load MATLAB 7.3 files:
我创建了一个小库来加载 MATLAB 7.3 文件:
pip install mat73
To load a .mat
7.3 into Python as a dictionary:
要将.mat
7.3 作为字典加载到 Python 中:
import mat73
data_dict = mat73.loadmat('data.mat')
simple as that!
就那么简单!