[SciPy-User] Deciphering Matlab Structures in Python
Matthew Brett
matthew.brett at gmail.com
Thu Jul 14 18:11:03 EDT 2011
Hi,
On Thu, Jul 14, 2011 at 10:56 PM, Paul Blelloch <paul.blelloch at ata-e.com> wrote:
> I used the scipy.io.loadmat function to read a Matlab save file
> that includes some structures. In particular my Matlab code looks something
> like:
>
> a=randn(10,10);
> e=eig(a);
> c.mat=a;
> c.eig=e;
> save
>
> When I read this in I have not trouble getting the a and e matrices, but
> when I look at the structure c I get something that looks like:
>
> array([[ ([[-1.918381876114684, 0.35909312220124379,
> 0.071481108399536447, 0.19413316926540342, -0.16044557980861471,
> -1.2431101120071242, 1.1728553895658809, 0.48682974787947486,
> -2.6862862203427951, 1.6479268546008081], [-0.13106782428104052,
> -0.79434474702678692, 0.15562063581539223, 0.29769161930929805,
> 0.37883517610546497, 0.13185918869913515, -2.1155833182251094,
> -0.4459044413697299, 1.5483561910050594, -0.30667757004699159],
> [-0.76863166681707873, -0.22731164066134216, -0.18219139268446782,
> -0.71012508212938263, -0.10716072249907738, -0.99947508351036363,
> 0.46216113959481137, 0.32567307426214315, -0.92437868406001578,
> -0.77404140889205997], [2.3899518363067971, 1.5938213548880649,
> 0.73101584111958162, -0.68767410135748297, 0.21283509315953025,
> -0.35421345818508981, -2.4599031616561144, -0.02009780648140742,
> -0.72040490902555765, -1.3542395842984021], [0.077245679591339572,
> 0.15520563382674926, -0.34756806580692184, 0.57283812483950614,
> 0.46806391670861758, 0.10558871537972472, -0.30485718475158774,
> -1.8884755753587876, 0.83350531035866249, -0.32592678918795287],
> [0.37560745023340852, 0.17859562900297904, 0.23442400798144603,
> 0.44521760367790375, -1.6160743410467069, 0.95542626643090245,
> 0.39823325391367814, 0.33836146832009201, -0.56649185628868159,
> 1.5532163153526557], [0.39556416936787731, -0.33773084599208325,
> -0.86772936409388435, -1.3259592591460707, 0.014738006197359526,
> 0.46479897799135333, -1.7057865505375613, 0.33780540287940392,
> -0.99915099624077919, 0.62813120976932557], [-0.11249908896048552,
> -1.5249918897593304, -0.98253858503060265, -1.4998754094582201,
> -0.037764347672058281, 0.26670638822706283, 0.75940981180842837,
> 0.42433121858231648, 0.64126705212804702, -1.0275461814124498],
> [-1.8290367022161331, -0.70940664667282161, -0.29357242375327913,
> 1.0159682271872925, 0.40380900009311393, -1.4232233423222125,
> 0.20323761868334428, 0.11287078734861246, -0.25395009085808468,
> -0.20194229402517888], [2.0914521934834784, -0.86661663365224473,
> 0.82446963631241454, -0.027515788346229055, 0.30525439285092776,
> 0.11105730902622701, 0.95868278992227296, -0.11827036575813449,
> -1.1216999904267995, 0.86106602555594924]],
> [[(-4.5696334670036887+0j)], [(3.7785600753832629+0j)],
> [(-2.5789412296806438+0j)],
> [(0.30103481909678725+1.8315300529614498j)],
> [(0.30103481909678725-1.8315300529614498j)],
> [(1.4965987808471322+0j)], [(-0.6068802651298042+1.19212639770675j)],
> [(-0.6068802651298042-1.19212639770675j)],
> [(-0.54808825947184969+0j)], [(0.19975366069054107+0j)]])]],
> dtype=[('mat', '|O4'), ('eig', '|O4')])
>
> Looking through the numpy documentation I can't figure out how to interpret
> that. How do I pull the fields out of the structure?
This is just a repeat of my reply on the Python X,Y list, just for reference.
What you got here was a structured array. You can get what you want
with something like:
>>> import scipy.io as sio
>>> ws = sio.loadmat('matlab.mat')
>>> c = ws['c']
You got this far already of course.
Notice that 'c' is a 2D array with a composite 'dtype':
>>> c.shape
(1, 1)
>>> c.dtype
dtype([('mat', '|O8'), ('eig', '|O8')]
To get the underlying fields you need:
>>> eig = c[0,0]['eig']
>>> mat = c[0,0]['mat']
To learn more it might be worth reading a little about structured
arrays in numpy.
Best,
Matthew
More information about the SciPy-User
mailing list