user177196
user177196

Reputation: 728

loading .mat and .m files with loadmat in Python

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: Unknown mat file type, version 101, 58

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.

Upvotes: 12

Views: 37137

Answers (6)

Keddrick Yue
Keddrick Yue

Reputation: 31

the scipy.io module can not read the .mat file that was saved by the newer h5py format.
You could try using the h5py module to load it.
Also, the channel priority is different: column first and row first

Upvotes: 0

arilwan
arilwan

Reputation: 3993

For people coming over this issue again. You can use numpy.loadtxt() to read .mat files generated by Octave.

For example. In Octave:

octave:1> arr1 = [1,2]
octave:2> arr2 = [2,3]
octave:3> save mydata.mat arr1 arr2

Then read mydata.mat in Python Interpreter:

>>> import numpy as np
>>> data = np.loadtxt('mydata.mat')
>>> data
array([[1., 2.],
       [2., 3.]])

Upvotes: 3

Sajjad Mohammadian
Sajjad Mohammadian

Reputation: 21

Much easier is to just load your data with:

numpy.loadtxt('your dot mat file')

Upvotes: 1

Yana Sosnovskaya
Yana Sosnovskaya

Reputation: 1

You can use MATLAB Engine API to run .m files and MATLAB functions. it can also load .mat files as well, but could be a bit slow. https://www.mathworks.com/help/matlab/matlab-engine-for-python.html

Upvotes: 0

Tasos Papastylianou
Tasos Papastylianou

Reputation: 22255

(converting my comments into an answer)

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.

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.

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.

EDIT: 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.]]

Upvotes: 7

O. Th. B.
O. Th. B.

Reputation: 1353

You cannot load .m script-files. These are MATLAB's own script-files and they contain MATLAB code. The function scipy.io.loadmat cannot parse this code.

On the other hand, the function can load data from a MATLAB .mat-file. This is MATLAB's own data-format within which you store variables and data - but not code.

Reference: https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html.

Upvotes: 3

Related Questions