converting python nested-list into matlab-cell using matlab engine

Viewed 158

I am trying to convert a python nested-list into a matlab-cell in python. My python nested-list contains only strings.

This is my list:

x = [['a', 'b'],['d','e']]                                                                                                 

type(x)                                                                                                                    
list

This was my attempt to pass it as matlab cell:

import matlab.engine 
import matlab 
eng = matlab.engine.start_matlab() 
y = eng.cell(x)                                                                                                            
type(y)                                                                                                                    
list

This approach fails, since the type of y is still a list.

Another attempt with an error:

y = eng.cellstr(x)
Error using cellstr (line 44)
Element 1 is not a string scalar or character array. All elements of cell input must be 
string scalars or character arrays.

Any suggestion here is appreciated, thanks in advance!

1 Answers

This approach fails, since the type of y is still a list.

That's just what the cell representation looks like in Python, but in reality that is a cell in MATLAB. You can confirm this by running a cell specific operation like cell2mat:

>>> import matlab.engine 
>>> import matlab

>>> eng = matlab.engine.start_matlab()     
>>> x = [['a', 'b'],['d','e']]
>>> matlab_cell = eng.cell(x)
>>> eng.cell2mat(matlab_cell[0])
'ab'

so matlab_cell is a cell for MATLAB:

>>> eng.iscell(matlab_cell)
True

but it is shown as a list in Python:

>>> type(matlab_cell)
<class 'list'>

You can find more information on the implicit conversion the engine does here.

Related