Read Matlab files with Python and Numeric?

nmw at ion.le.ac.uk nmw at ion.le.ac.uk
Mon Sep 24 09:05:20 EDT 2001


gb at cs.unc.edu writes:

> Does anyone have an extension for Python+Numeric to read Matlab format
> .mat files? I'm trying to get off Matlab after years of use and have
> lots of data files that I would like to read.
> 
> Thanks
> gb


Yes. I have a module which will load MATLAB files. It's not entirely 
complete but handles most MATLAB files (the most obvious omission is
sparse matrices).

It requires NumPy as the MATLAB matrices are loaded as Numeric arrays.
For MATLAB 5 files, cells are loaded as lists and structures as
dictionaries.

If the entire contents of the .mat file are loaded then they are returned
in a dictionary rather than being inserted into the current workspace as
MATLAB does.

e.g.

>>> import Numeric
>>> import matfile

>>> a=matfile.load('tau0_GUP_202_12.8.mat')

To list the matrices loaded:

>>> a.keys()
['Nkill', 'c_addr1', 's_addr1', 'datalen', 'c_addr2', 's_addr2', 'ngup', 
'b_addr1', 'b_addr2', 'Sysconst']


or you can load specific matrices which will be returned in a list in the
order requested, e.g. to load the datalen and ngup matrices :

>>> b=matfile.load('tau0_GUP_202_12.8.mat','datalen','ngup')

>>> b
[array([ 31632.]), array([ 26278.])]

>>> a['datalen'] 
array([ 31632.])
>>> a['ngup']
array([ 26278.])

>>> b[0][0]
31632.0
>>> a['datalen'][0]
31632.0


If you would like more info e-mail me and I can send details.

-- 
-----------------------------------------------------------
Nigel Wade, System Administrator, Space Plasma Physics Group,
            University of Leicester, Leicester, LE1 7RH, UK 
E-mail :    nmw at ion.le.ac.uk 
Phone :     +44 (0)116 2523568, Fax : +44 (0)116 2523555



More information about the Python-list mailing list