0

I need to save some data from Python and read it later with both Python and Matlab. The obvious choice seems to be scipy.io.savemat. But look what happens:

>>> a = {'foo': 0, 'bar': [1,2,3,4,5], 'baz': {'key1': range(10), 'key2': 'Oh No
 Mr Bill!'}}
>>> scipy.io.savemat('a.mat',a)
>>> b = scipy.io.loadmat('a.mat')
>>> b['foo']
array([[0]])
>>> b['bar']
array([[1],
       [2],
       [3],
       [4],
       [5]])
>>> b['baz']
array([[([u'Oh No Mr Bill!'], [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
)]],
      dtype=[('key2', '|O4'), ('key1', '|O4')])
>>> b['baz']['key1']
array([[[[0]
 [1]
 [2]
 [3]
 [4]
 [5]
 [6]
 [7]
 [8]
 [9]]]], dtype=object)
>>> b['baz']['key2']
array([[[u'Oh No Mr Bill!']]], dtype=object)
>>> b['baz']['key2'][0,0]
array([u'Oh No Mr Bill!'],
      dtype='<U14')

Everything seems to be stored as 2D arrays for each dictionary... (e.g. foo['baz']['key1'] shows up as a 4D array) Is this a bug or is there a reason for this behavior?

Jason S
  • 184,598
  • 164
  • 608
  • 970

1 Answers1

0

Is there a reason that you can't use the pickle or marshall modules in combination with the matlab format?

E.g. Save the data in both matlab format (losing structural information (e.g. dicts)) and also pickle it at the same time for lossless re-import into python

Tom Dalton
  • 6,122
  • 24
  • 35
  • Yes. Read the first sentence of my question: "I need to save some data from Python and read it later with both Python **and Matlab**." – Jason S May 31 '13 at 16:03
  • Matlab's data model does not match Python's (e.g. with respect to dictionaries). So you are essentially losing information when you serialise python objects/data to the Matlab format. – Tom Dalton May 31 '13 at 16:20
  • ...and that's fine, I just want to be able to recover the data from Matlab in a way that is useful rather than confusing. (in other words, I'm not expecting to serialize custom objects or functions or cyclical graphs, but for the basic containers I want to know how to read them back in.) – Jason S May 31 '13 at 17:27