1

Thanks to scipy.io it is fast and easy to pass from Python to MATLAB, objects like stuctures or cell arrays.

MATLAB structure:


  • in Python:

import scipy.io as sio

titi={'oui': 'Y', 'non': 'N', 'AgeDuCapitaine': 53}
sio.savemat('titi.mat', {'titi': titi})

  • in Matlab:

load('titi')
titi

    titi = 

    AgeDuCapitaine: 53
               oui: 'Y'
               non: 'N'

titi.AgeDuCapitaine

    ans =

                   53

MATLAB cell array:


  • in Python:

import scipy.io as sio
import numpy as np

tutu=np.zeros((3,), dtype=np.object)
tutu[0]=1
tutu[1]='omg'
tutu[2]=np.zeros((2,), dtype=np.object)
tutu[2][0]='vrai'
tutu[2][1]=2
sio.savemat('tutu.mat', {'tutu': tutu})

  • in Matlab:

load('tutu')
tutu

    tutu = 

    [1]    'omg'    {1x2 cell}

tutu{1}

    ans =

                1

tutu{3}{1}

    ans =

                vrai

However, let's say that we want to pass an object corresponding to a mixed MATLAB cell array of structures, as an example a final MATLAB object like:
toto{1}.weapon{2}.Name='fleurs' ...

MATLAB cell array of structures:


in python (test, not yet convincing !):

import scipy.io as sio
import numpy as np

toto = np.zeros((2,), dtype=np.object)
toto[0] = {}
toto[1] = {}
toto[0]['weapon'] = np.zeros((2,), dtype=np.object)
toto[0]['weapon'][0] = {}
toto[0]['weapon'][1] = {}
toto[0]['weapon'][1]['Name'] = 'fleurs'
toto

   array([{'weapon': array([{}, {'Name': 'fleurs'}], dtype=object)}, {}], dtype=object)

sio.savemat('toto.mat', {'toto':toto})

   Traceback (most recent call last):
       File "<stdin>", line 1, in <module>
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio.py", line 207, in savemat
         MW.put_variables(mdict)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 876, in put_variables
         self._matrix_writer.write_top(var, asbytes(name), is_global)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 626, in write_top
         self.write(arr)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 655, in write
         self.write_cells(narr)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 759, in write_cells
         self.write(el)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 653, in write
         self.write_struct(narr)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 764, in write_struct
         self._write_items(arr)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 782, in _write_items
         self.write(el[f])
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 655, in write
         self.write_cells(narr)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 759, in write_cells
         self.write(el)
       File "/usr/lib64/python2.7/site-packages/scipy/io/matlab/mio5.py", line 647, in write
         % (arr, type(arr)))
   TypeError: Could not convert {} (type <type 'dict'>) to array

So, is it possible to create in Python and pass to MATLAB a cell array of structures ?
Did I make a mistake ?

1 Answer 1

1

Ooops I just understood why the TypeError exception was raised ... In fact, this is the right way to pass a cell array of structures from Python to MATLAB but ... It seems that the scipy.io.savemat does not allow empty dictionary. Indeed If I complet all the dictionnaries all is working fine !:

In Python:

>>> import scipy.io as sio   
>>> import numpy as np

>>> toto = np.zeros((2,), dtype=np.object)
>>> toto[0]={}
>>> toto[1]={}
>>> toto[1]['Site']=['Tataouine']
>>> toto[0]['weapon'] = np.zeros((2,), dtype=np.object)
>>> toto[0]['weapon'][0]={}
>>> toto[0]['weapon'][1]={}
>>> toto[0]['weapon'][1]['Name']='fleurs'
>>> toto[0]['weapon'][0]['Name']='bonbons'
>>> toto
array([ {'weapon': array([{'Name': 'bonbons'}, {'Name': 'fleurs'}], dtype=object)},
       {'Site': ['Tataouine']}], dtype=object)
>>> sio.savemat('toto.mat', {'toto':toto})

In MATLAB:

>> load('toto')
>> toto

toto = 

    [1x1 struct]    [1x1 struct]

>> toto{1}.weapon{2}.Name

ans =

fleurs

Hope this will help you to not waste your time with this silly problem !!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.