Passing from Python to MATLAB a cell array of structures
Show older comments
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 ?
Answers (1)
Eric Condamine
on 14 Mar 2017
3 votes
1 Comment
Ryland Mathews
on 17 Jul 2019
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))
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!