2
votes

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
Any updates? .... - Paolo
Hi @Paolo, apologies for the silence. Actually, I need to wrap a matlab function (f_matlab) inside python. The input of f_matlab is a matlab cell structure. It would look like this: x_mat = 2×2 cell array {'a'} {'b'} {'c'} {'d'} ` However, wrapping f_matlab inside python as eng.f_matlab(eng.cell(x)) fails. Because eng.cell(x) is not the same as x_mat. Its size and elements are different. This is my struggle ;) - Şeyma Bayrak

1 Answers

0
votes

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.