Scipy: io.loadmat not working on MATLAB ASCII files (Trac #1319)

Created on 25 Apr 2013  路  4Comments  路  Source: scipy/scipy

_Original ticket http://projects.scipy.org/scipy/ticket/1319 on 2010-10-25 by trac user daviesk24, assigned to unknown._

I am just getting started with SciPy. I'd like to use it to load data that a stand-alone program is saving as a MATLAB version 4 in ASCII. I've isolated the problem a little bit. I'd appreciate any help. Thanks!

In MATLAB, try:

a = 1:10;
save('test_binary.mat', 'a')
save('-ascii', 'test_ascii.mat', 'a')

Then in Python, try:

import scipy.io

print "Trying to load test_binary.mat..."
b = scipy.io.loadmat("test_binary.mat", struct_as_record=True)
print b

print "\nTrying to load test_ascii.mat..."
c = scipy.io.loadmat("test_ascii.mat", struct_as_record=True)
print c

I get the following (MATLAB R2007A and Python v2.6 on Ubuntu 10.04):

Trying to load test_binary.mat...
{'a': array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10]], dtype=uint8), '__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file, Platform: GLNX86, Created on: Mon Oct 25 11:59:50 2010', '__globals__': []}

Trying to load test_ascii.mat...
Traceback (most recent call last):
  File "./load_data.py", line 21, in <module>
    c = scipy.io.loadmat("test_ascii.mat", struct_as_record=True)
  File "/usr/lib/python2.6/dist-packages/scipy/io/matlab/mio.py", line 110, in loadmat
    MR = mat_reader_factory(file_name, appendmat, **kwargs)
  File "/usr/lib/python2.6/dist-packages/scipy/io/matlab/mio.py", line 80, in mat_reader_factory
    mjv, mnv = get_matfile_version(byte_stream)
  File "/usr/lib/python2.6/dist-packages/scipy/io/matlab/miobase.py", line 125, in get_matfile_version
    raise ValueError('Unknown mat file type, version %s' % ret)
TypeError: not all arguments converted during string formatting

I've tried all the settings of byte_order in loadmat(), but none seem to work.

Migrated from Trac defect prio-lowest scipy.io

Most helpful comment

_@pv wrote on 2010-10-25_

Matlab ASCII files are not MAT-files but plain text files.
You should use numpy.loadtxt to load those.

All 4 comments

_trac user daviesk24 wrote on 2010-10-25_

I'm sorry--I shouldn't have posted so quickly. I was able to load the MATLAB file. What I posted earlier may or may not be an issue, but it turned out that my problem was different. The MATLAB file had a string array with unsupported characters in it ("茅"). Once I removed that, it worked.

_@pv wrote on 2010-10-25_

Matlab ASCII files are not MAT-files but plain text files.
You should use numpy.loadtxt to load those.

numpy.loadtxt does not work for me.

This works for me: MATLAB

>> a = randn(10, 7);
>> save a.txt a -ascii

IPython:

In [1]: import numpy as np
In [2]: a = np.loadtxt('a.txt')
Was this page helpful?
0 / 5 - 0 ratings