[SciPy-dev] Please stress-test SVN version pf matlab reader

Matthew Brett matthew.brett at gmail.com
Sat Jan 9 22:39:46 EST 2010


Hi Ariel,

> further - if I set both squeeze_me and struct_as_record to True (does that
> make sense?), I get into some rather nasty situations, where I can't use the
> variables in the matfile:
>
> For example:
>
> In [40]: m =
> sio.loadmat('C-RA_Adapt.mat',squeeze_me=True,struct_as_record=True)
>
> In [41]: h = m['history']
>
> In [42]: h1 = h[0]
>
> In [43]: q = h1['q']
...
> There's supposed to be an array called 'x' in there:
>
> In [46]: q['x']
> ---------------------------------------------------------------------------
> ValueError                                Traceback (most recent call last)

Looking further into this - rather late at night - so I might be wrong
- I think this is structural (forgive the pun) to structured arrays.
We have to load the arrays as structured arrays with named fields and
object dtype per field, because matlab allows any contents of the
fields (fields need not have the same class of contents for each
struct in the struct array).  When we squeeze structured arrays, we
get 0d structured arrays, with object fields.  We can't now index
these, but It is only indexing into the structured array, that
dereferences the object:

In [3]: st = np.zeros((1,), dtype=[('f0', 'O'), ('f1', 'O')])

In [4]: st[0]['f0']
Out[4]: 0

In [5]: st['f0']
Out[5]: array([0], dtype=object)

Now, when we squeeze to 0d, we can't index any more:

In [6]: sst = np.squeeze(st)

In [7]: sst.shape
Out[7]: ()

In [8]: sst['f0']
Out[8]: array(0, dtype=object)

So we can't dereference, except explicitly like this:

In [9]: sst['f0'].item()
Out[9]: 0

I think.   Well, more, I hope that someone who knows better than I do
can think of a way round this...

Best,

Matthew



More information about the SciPy-Dev mailing list