[SciPy-User] scipy.io.loadmat error when trying to read a .mat file

Matthew Brett matthew.brett@gmail....
Tue Oct 12 00:40:46 CDT 2010


On Wed, Oct 6, 2010 at 6:36 AM, Dharhas Pothina
<Dharhas.Pothina@twdb.state.tx.us> wrote:
> fun fun fun.

Oh yes.

> I can't see the second 'System' variable in Matlab R14.

No, I guess matlab does the same as we do, and just overwrites with
the last occurrence of the variable.

> In the meanwhile is there a work around to load the rest of the data file into scipy.

Yes - I can now read both your files into scipy.  I just did
'fieldname', '_1_fieldname', '_2_fieldname' ... for the duplicates, so
it's a bit of a hidden feature, but it does seem to work.  You need to
clone my github copy:

git clone git://github.com/matthew-brett/scipy-work.git
git checkout -b mio-read-fix origin/mio-read-fix

and install scipy from there.  Does that work for you?

Incidentally, if you see the scipy/io/matlab/mio5.py file you'll see a
function 'varmats_from_mat' that takes a given open mat file and
returns a list of individual variables as (name, mat_stream) tuples,
which you can then read out with loadmat. On the lines of:

from scipy.io.matlab.mio5 import varmats_from_mat
varmats = varmats_from_mat(open('strange_file.mat', 'rb'))

and then

first_vars = loadmat(varmats[0][1])

That last will give you the contents of the first variable.

> Is it possible from a technical point of view to mimic the matlab load command that lets you specify which variables to load:

Yes, almost anything is possible, but, oh, a little bit annoying to
implement unless it's really a killer feature for someone.  I'm hoping
that now we can read your files, maybe it's a bit less killer for you?

You can already specify a variable name (not a regexp), so for example:

from scipy.io.matlab.mio5 import MatFile5Reader
rdr = MatFile5Reader(open('20100819125947.mat', 'rb'))
var = rdr.get_variables(['System'])

will already return you the first 'System' variable, that would
otherwise be overwritten when you read the full set of variables.  You
are right, that should be in the loadmat API somewhere.

See you,


More information about the SciPy-User mailing list