Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Read Matlab files with Python and Numeric?

0 views
Skip to first unread message

g...@cs.unc.edu

unread,
Sep 21, 2001, 3:21:41 PM9/21/01
to
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

n...@ion.le.ac.uk

unread,
Sep 24, 2001, 9:05:20 AM9/24/01
to
g...@cs.unc.edu writes:


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 : n...@ion.le.ac.uk
Phone : +44 (0)116 2523568, Fax : +44 (0)116 2523555

n...@ion.le.ac.uk

unread,
Sep 24, 2001, 12:03:40 PM9/24/01
to

I've had a few requests for this, so I've tarballed the source and put it on
our FTP server. It's part of a much, much larger project, but I've extracted
the relevent bits and put them together in the tarfile.

You can get the tarball from ftp://ion.le.ac.uk/matfile/matfile.tar.gz.

To build the module extract the tarball - it will create a directory called
matfile containing the source. Edit the Makefile and change the location
of the Python and Numerc header files and libraries for your system, the
macros which need changing are PYTHON_INLUCDE and PYTHON_LIBDIR and
PYTHON_LIBS.

Type "make" and it should build the matfile library first and then the
matfilemodule.so.

Oh, BTW, I should say that this is set up for Linux. No-one has mentioned
environment. For other *NIX systems it will be necessary to change some
of the compiler/linker options for building and naming shared objects.
I don't have a clue what would be necessary for a Windows environment.

Travis Oliphant

unread,
Oct 2, 2001, 7:53:05 PM10/2/01
to n...@ion.le.ac.uk
>
> 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.

I would like to see your implentation. I just finished writing a MAT file
reader and writer for inclusion in SciPy. (I should have checked the
newsgroups first...) My version only handles Level 4 MAT files.

-Travis


0 new messages