Passing from Python to MATLAB a cell array of structures

39 ビュー (過去 30 日間)
Eric Condamine
Eric Condamine 2017 年 3 月 14 日
コメント済み: Ryland Mathews 2019 年 7 月 17 日
Let's say that we want to pass from Python 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' ...
I tried in Python:
>>> 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 件)

Eric Condamine
Eric Condamine 2017 年 3 月 14 日

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 !!

  1 件のコメント
Ryland Mathews
Ryland Mathews 2019 年 7 月 17 日
I am currently pulling in a pandas dataframe (of strings) into matlab using code like below:
is there a way to then convert the dataframe into usuable form to plot in a matlab table, I tried for hours to convert it and couldnt get it done.
clear classes
mod = py.importlib.import_module('test')
py.importlib.reload(mod)
cP = py.list(py.test.FlowTracker().todays_portfolio(1,10))

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by