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

Matthew Brett matthew.brett at gmail.com
Tue Oct 5 00:41:10 EDT 2010


Hi,

> I see that, if I load the 20100819102442.mat file in matlab and save
> it again, loadmat can read it.   This is in matlab r2008a.

Just a little more on the file 20100819102442.mat

I suspect it must be being written from outside matlab.

The error you are getting is because there is a 1-length string which
has a 0 byte length.  Matlab copes OK with this, and returns a string
' ' - here for the System.Units struct 'Sample' field:

          Pitch: 'deg'
           Roll: 'deg'
        Heading: 'deg'
    Temperature: 'degC'
           Time: ' '
            SNR: 'dB'
         Sample: ' '
        Voltage: 'V'
     Cell_Start: 'm'
      Cell_Size: 'm'
           Step: ' '
          Pings: ' '

scipy and octave barf.  I don't know what scipy used to do in that
situation, but I would be surprised if it hasn't always barfed.   It's
easy to fix that one, but then, once fixed, you get this:

/Users/mb312/usr/local/lib/python2.6/site-packages/scipy/io/matlab/mio5_utils.so
in scipy.io.matlab.mio5_utils.VarReader5.read_struct
(./mio5_utils.c:6836)()

ValueError: two fields with the same name

That turns out to be because the 'Summary' struct has 4 fields all
named 'Station_Q':

              Top_Q: [34x1 double]
           Middle_Q: [34x1 double]
           Bottom_Q: [34x1 double]
             Left_Q: [34x1 double]
            Right_Q: [34x1 double]
            Total_Q: [34x1 double]
              Depth: [34x1 double]
              Cells: [34x1 double]
              Track: [34x2 double]
           Mean_Vel: [34x2 double]
           Boat_Vel: [34x4 double]
          Station_Q: [34x1 double]
          Station_Q: [34x1 double]
          Station_Q: [34x1 double]
          Station_Q: [34x1 double]

Matlab can handle that, although I don't know how you can make a
structure that has two fields with the same name - as far as I know
there's no way to do that with matlab syntax, e.g:

>> a = struct('field1', 1, 'field1', 2)
??? Error using ==> struct
Duplicate field name "field1"

I don't know how to return those guys to you either - structs come
back either as record arrays or objects, and neither allow duplicate
names.  Of course we could rename the duplicates, but that would start
to get a bit tangled.

Is there any way you can load these into matlab and fix them up before
loading into scipy?

I'll commit the changes to allow the 1 length 0 byte strings tomorrow.

Best,

Matthew



More information about the SciPy-User mailing list