title: Passing from Python to MATLAB a cell array of structures
body: <p><strong><em>Thanks to <a href="https://docs.scipy.org/doc/scipy/reference/tutorial/io.html" rel="nofollow noreferrer">scipy.io</a> it is fast and easy to pass from Python to MATLAB, objects like stuctures or cell arrays.</em></strong></p>
  
  <p>MATLAB structure:</p>
  
  <hr>
  
  <ul>
  <li>in Python:</li>
  </ul>
  
  <blockquote>
    <p>import scipy.io as sio   </p>
    
    <p>titi={'oui': 'Y', 'non': 'N', 'AgeDuCapitaine': 53}<br>
    sio.savemat('titi.mat', {'titi': titi})</p>
  </blockquote>
  
  <ul>
  <li>in Matlab:</li>
  </ul>
  
  <blockquote>
    <p>load('titi')<br>
    titi</p>
  </blockquote>
  
  <pre><code>    titi = 
  
      AgeDuCapitaine: 53
                 oui: 'Y'
                 non: 'N'
  </code></pre>
  
  <blockquote>
    <p>titi.AgeDuCapitaine</p>
  </blockquote>
  
  <pre><code>    ans =
  
                     53
  </code></pre>
  
  <p>MATLAB cell array:</p>
  
  <hr>
  
  <ul>
  <li>in Python:</li>
  </ul>
  
  <blockquote>
    <p>import scipy.io as sio<br>
    import numpy as np</p>
    
    <p>tutu=np.zeros((3,), dtype=np.object)<br>
    tutu[0]=1<br>
    tutu[1]='omg'<br>
    tutu[2]=np.zeros((2,), dtype=np.object)<br>
    tutu[2][0]='vrai'<br>
    tutu[2][1]=2<br>
    sio.savemat('tutu.mat', {'tutu': tutu})    </p>
  </blockquote>
  
  <ul>
  <li>in Matlab:</li>
  </ul>
  
  <blockquote>
    <p>load('tutu')<br>
    tutu</p>
  </blockquote>
  
  <pre><code>    tutu = 
  
      [1]    'omg'    {1x2 cell}
  </code></pre>
  
  <blockquote>
    <p>tutu{1}</p>
  </blockquote>
  
  <pre><code>    ans =
  
                  1
  </code></pre>
  
  <blockquote>
    <p>tutu{3}{1}</p>
  </blockquote>
  
  <pre><code>    ans =
  
                  vrai
  </code></pre>
  
  <p><strong><em>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:</em></strong><br>
  <strong><em>toto{1}.weapon{2}.Name='fleurs' ...</em></strong></p>
  
  <p>MATLAB cell array of structures:</p>
  
  <hr>
  
  <p>in python (test, not yet convincing !):</p>
  
  <blockquote>
    <p>import scipy.io as sio<br>
    import numpy as np    </p>
    
    <p>toto = np.zeros((2,), dtype=np.object)<br>
    toto[0] = {}<br>
    toto[1] = {}<br>
    toto[0]['weapon'] = np.zeros((2,), dtype=np.object)<br>
    toto[0]['weapon'][0] = {}<br>
    toto[0]['weapon'][1] = {}<br>
    toto[0]['weapon'][1]['Name'] = 'fleurs'<br>
    toto    </p>
  </blockquote>
  
  <pre><code>   array([{'weapon': array([{}, {'Name': 'fleurs'}], dtype=object)}, {}], dtype=object)
  </code></pre>
  
  <blockquote>
    <p>sio.savemat('toto.mat', {'toto':toto})     </p>
  </blockquote>
  
  <pre><code>   Traceback (most recent call last):
         File "&lt;stdin&gt;", line 1, in &lt;module&gt;
         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 &lt;type 'dict'&gt;) to array
  </code></pre>
  
  <p><strong><em>So, is it possible to create in Python and pass to MATLAB a cell array of structures ?<br>
  Did I make a mistake ?</em></strong>    </p>