[Numpy-discussion] scipy.io.loadmat can't handle structs from octave

Brian Blais bblais at bryant.edu
Mon Jun 12 07:56:51 CDT 2006


Hello,

I am trying to load some .mat files in python, that were saved with octave.  I get
some weird things with strings, and structs fail altogether.  Am I doing something
wrong?  Python 2.4, Scipy '0.4.9.1906', numpy 0.9.8, octave 2.1.71, running Linux.

thanks,

	Brian Blais


here is what I tried:

Numbers are ok:

     ========OCTAVE==========
         >> a=rand(4)
         a =

           0.617860  0.884195  0.032998  0.217922
           0.207970  0.753992  0.333966  0.905661
           0.048432  0.290895  0.353919  0.958442
           0.697213  0.616851  0.426595  0.371364

         >> save -mat-binary pythonfile.mat a
     =========PYTHON===========

         In [13]:d=io.loadmat('pythonfile.mat')

         In [14]:d
         Out[14]:
         {'__header__': 'MATLAB 5.0 MAT-file, written by Octave 2.1.71, 2006-06-09
14:23:54 UTC',
          '__version__': '1.0',
          'a': array([[ 0.61785957,  0.88419484,  0.03299807,  0.21792207],
                [ 0.20796989,  0.75399171,  0.33396634,  0.90566095],
                [ 0.04843219,  0.29089527,  0.35391921,  0.95844178],
                [ 0.69721313,  0.61685075,  0.42659485,  0.37136358]])}


Strings are weird (turns to all 1's)
     ========OCTAVE==========
         >> a='hello'
         a = hello
         >> save -mat-binary pythonfile.mat a
     =========PYTHON===========
         In [15]:d=io.loadmat('pythonfile.mat')

         In [16]:d
         Out[16]:
         {'__header__': 'MATLAB 5.0 MAT-file, written by Octave 2.1.71, 2006-06-09
14:24:13 UTC',
          '__version__': '1.0',
          'a': '11111'}


Cell arrays are fine (except for strings):
     ========OCTAVE==========
         >> a={5 [1,2,3] 'this'}
         a =

         {
           [1,1] = 5
           [1,2] =

             1  2  3

           [1,3] = this
         }

         >> save -mat-binary pythonfile.mat a
     =========PYTHON===========
         In [17]:d=io.loadmat('pythonfile.mat')

         In [18]:d
         Out[18]:
         {'__header__': 'MATLAB 5.0 MAT-file, written by Octave 2.1.71, 2006-06-09
14:24:51 UTC',
          '__version__': '1.0',
          'a': array([5.0, [ 1.  2.  3.], 1111], dtype=object)}




Structs crash:
     ========OCTAVE==========
         >> clear a
         >> a.hello=5
         a =
         {
           hello = 5
         }

         >> a.this=[1,2,3]
         a =
         {
           hello = 5
           this =

             1  2  3

         }

         >> save -mat-binary pythonfile.mat a
     =========PYTHON===========
         In [19]:d=io.loadmat('pythonfile.mat')
         ---------------------------------------------------------------------------
         exceptions.AttributeError                            Traceback (most recent
call last)

         /home/bblais/octave/work/mouse/<console>

         /usr/lib/python2.4/site-packages/scipy/io/mio.py in loadmat(name, dict,
appendmat, basename)
             751     if not (0 in test_vals):       # MATLAB version 5 format
             752         fid.rewind()
         --> 753         thisdict = _loadv5(fid,basename)
             754         if dict is not None:
             755             dict.update(thisdict)

         /usr/lib/python2.4/site-packages/scipy/io/mio.py in _loadv5(fid, basename)
             688         try:
             689             var = var + 1
         --> 690             el, varname = _get_element(fid)
             691             if varname is None:
             692                 varname = '%s_%04d' % (basename,var)

         /usr/lib/python2.4/site-packages/scipy/io/mio.py in _get_element(fid)
             676
             677     # handle miMatrix type
         --> 678     el, name = _parse_mimatrix(fid,numbytes)
             679     return el, name
             680

         /usr/lib/python2.4/site-packages/scipy/io/mio.py in _parse_mimatrix(fid, bytes)
             597                 result[i].__dict__[element] = val
             598         result = squeeze(transpose(reshape(result,tupdims)))
         --> 599         if rank(result)==0: result = result.item()
             600
             601         # object is like a structure with but with a class name

         AttributeError: mat_struct instance has no attribute 'item'


-- 
-----------------

              bblais at bryant.edu
              http://web.bryant.edu/~bblais









More information about the Numpy-discussion mailing list