在Python中使用loadmat加载.mat和.m文件 [英] loading .mat and .m files with loadmat in Python
问题描述
我目前正在学习神经网络课程,并且尝试使用scipy.io.loadmat(filename)
从Python加载.mat
文件,但是我不断收到以下错误消息:
I am currently taking a Neural Network course, and I am trying to load a .mat
file from Python using scipy.io.loadmat(filename)
, but I keep getting the following error message:
ValueError:未知的Mat文件类型,版本101、58
ValueError: Unknown mat file type, version 101, 58
当我尝试使用scipy.io.loadmat()
加载.m文件而不是.mat
文件时,出现了相同的消息.
The same message occured when I tried loading a .m file, rather than .mat
file, using scipy.io.loadmat()
.
我不知道如何解决此问题,因此如果有人在这里可以帮助我,我将不胜感激.
I don't know how to resolve this issue, so I would really appreciate if someone on here could help me out.
推荐答案
(将我的评论转换为答案)
.m
文件是matlab/八度音阶脚本.您无需加载"脚本.甚至在Matlab/八度音阶中也没有.您只能运行它,这可能会或可能不会导致特定的工作空间,然后可以将其保存到.mat
文件中.
A .m
file is a matlab / octave script. You don't "load" a script. Not even in matlab / octave. You can only run it, and this may or may not result in a particular workspace that you can then save into a .mat
file.
您可以使用python的"matlab接口"从python会话中运行.m
文件,例如官方适用于Python的MATLAB引擎API ,或者您有较旧的matlab版本,可以尝试在线查找其他python matlab接口(一个简单的Google搜索给了我 pymatlab , mlab , oct2py 已知运作良好的介面.一旦安装了这些接口之一,就可以尝试运行脚本,然后将工作区变量传输到python上,或者使用该接口保存到.mat文件,然后像往常一样使用scipy.io.loadmat
将其加载回python
There are "matlab interfaces" for python that you could use to run an .m
file from within a python session, e.g. the official MATLAB Engine API for Python, or if you have an older matlab version you could try finding other python matlab interfaces online (a simple google search gave me pymatlab, mlab, mlabwrap etc). If your .m file is octave-compatible (and you have octave installed) you could also try the oct2py interface which is known to work well. Once you have one of those interfaces installed, you could either attempt to run the script and then transfer the workspace variables onto python, or save to a .mat file using the interface, and then load it back onto python using scipy.io.loadmat
as normal.
至于.mat文件给您一个错误,最可能的原因是它是.mat规范的较新版本,而python尚无法识别(您使用的是哪个matlab版本?).尝试将.mat文件加载到matlab(或八度)中,然后使用-v7选项再次保存.然后,Python应该没有问题地打开它.
As for the .mat file giving you an error, the most likely reason for this is that it's a newer version of the .mat specification, which python doesn't recognise yet (what matlab version are you using?). Try loading the .mat file in matlab (or octave) and saving it again with the -v7 option. Python should then open this without problems.
这是一个通过oct2py运行八度脚本的示例,将生成的八度工作区保存到.mat
文件中,并将.mat
文件加载到python中.
Here's an example running an octave script via oct2py, saving the resulting octave workspace onto a .mat
file, and loading the .mat
file into python.
%% file myscript.m located at /home/tasos/Desktop
a = 1
b = 2
c = 3
# from python:
from oct2py import octave as oct
oct.eval("cd /home/tasos/Desktop")
oct.eval("myscript")
oct.eval("save -v7 myworkspace.mat")
from scipy.io import loadmat
D = loadmat("/home/tasos/Desktop/myworkspace.mat")
print D.keys()
#>>> ['a', 'c', 'b', '__header__', '__globals__', 'ans', '__version__']
print D['a'], D['b'], D['c']
#>>> [[ 1.]] [[ 2.]] [[ 3.]]
这篇关于在Python中使用loadmat加载.mat和.m文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!