Load pickle files in Matlab

It’s easy enough to load .mat files in Python via the scipy.io.loadmat function. But what about loading .pickle files into Matlab? That’s easy enough by calling a system command in Matlab, like so:

function [a] = loadpickle(filename)
    if ~exist(filename,'file')
        error('%s is not a file',filename);
    outname = [tempname() '.mat'];
    pyscript = ['import cPickle as pickle;import sys;import scipy.io;file=open("' filename '","r");dat=pickle.load(file);file.close();scipy.io.savemat("' outname '",dat)'];
    system(['LD_LIBRARY_PATH=/opt/intel/composer_xe_2013/mkl/lib/intel64:/opt/intel/composer_xe_2013/lib/intel64;python -c ''' pyscript '''']);
    a = load(outname);

Note that I’m setting the LD_LIBRARY_PATH environment variable since Matlab seems to reset this variable internally and it causes .so module import errors in Python.


  1. When I try this I seem to be getting this error – File “/path-to-python-packages/scipy/io/matlab/mio5.py”, line 857, in put_variables
    for name, var in mdict.items():
    AttributeError: ‘list’ object has no attribute ‘items’
    I am using scipy 0.13 – Any Idea what could be going wrong here ?

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s