[SciPy-user] common storage between matlab and python

Albert Strasheim fullung@gmail....
Tue May 6 01:10:15 CDT 2008


On Tue, May 6, 2008 at 5:26 AM, Roger Herikstad
<roger.herikstad@gmail.com> wrote:
> Hi list,
>   Does anyone know of a matlab i/o interface beyond that of
>  scipy.io.loadmat /savemat? I know these routines will handle scalars
>  and vector, but what about matlab structures? My problem is that we
>  have a substantial amount of code written in matlab that makes use of
>  matlab's object oriented programming, storing the results of various
>  calculations in objects. What I would like to do is to interface with
>  these objects in python, that is read them from disk, do some
>  calculations, and write them back in a format consistent with what the
>  matlab object expects. So, for instance for the matlab object obj, the
>  data is stored as obj.data.field1, obj.data.field2, etc. Is there a
>  way for me to read the matlab file, do something to field1 and field2,
>  in python, then store the modified fields back into the structure for
>  matlab to read? Thanks!

If you don't find a solution specific to MATLAB, I'd recommend using HDF5.

Using HDF5 allows me to move my "objects" seamlessly between MATLAB,
Python and Java.

You'll have to write a bit of MATLAB code to take a struct and inspect
it using setfield, getfield and fieldnames, and then use hdf5write to
write it to disk.

obj might be stored in the HDF5 file obj.h5, with a "data" group, and
two datasets ("field1" and "field2").

Then you can load it up in PyTables, which should give you an
interface very similar to what you have in MATLAB. I think
obj.data.field1 might just work thanks to the magic that is PyTables
(where field1 could be a returned as a NumPy array).

Hope this helps.



More information about the SciPy-user mailing list