Mon Jan 4 16:33:30 EST 2010

Saving MATLAB .mat files in python

I've been working on how to save MATLAB .mat files using python’s scipy module. Here are some hints for using the scipy.io.savemat command. There is a nice tutorial on the subject.

In python:

import scipy.io as p

nav = {}         # this is a dictionary
nav['lat'] = 38.4 
nav['lon'] = -70.1 

p.savemat('test.mat',nav)

In MATLAB

load test.mat
whos

lat lon

Ok so now, how do we save a MATLAB structure? After some research it seems I need a scipy update. My current version (as part of my Enthought Python Distribution is 0.6. The current version supplied by the folks at Enthought is 0.8 and the tutorial above seems to be using 0.7. So standby…

YES! It works! Here’s how:

import scipy.io as p

data = {}
nav = {}
nav['lat'] = 38.4 
nav['lon'] = -70.1 
data['nav'] = nav

p.savemat('test.mat',data)

In MATLAB

>load test.mat
>nav

nav = 

lat: 38.399999999999999
lon: -70.099999999999994

Perfect!


Posted by Val Schmidt | Permanent link