필터 지우기
필터 지우기

Passing from Python to MATLAB a cell array of structures

조회 수: 38 (최근 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 CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by