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 nptutu=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 nptoto = 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 ?