Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 827
user177196 Avatar asked Aug 05 '17 18:08

user177196


People also ask

Is .m and .MAT same?

Files with a . m extension contain MATLAB code, either in the form of a script or a function. Files with a . mat extension contain MATLAB formatted data, and data can be loaded from or written to these files using the functions load and save , respectively.

How do I load data from a .MAT file?

If filename is a MAT-file, then load(filename) loads variables in the MAT-file into the MATLAB® workspace. If filename is an ASCII file, then load(filename) creates a double-precision array containing data from the file.


1 Answers

(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.]]
like image 56
Tasos Papastylianou Avatar answered Nov 03 '22 00:11

Tasos Papastylianou