Converting a Python list into a MATLAB cellstr through Python MATLAB engine
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello there,
I have a list of paths to files (so basically strings) in Python, for example:
#python code
lst = ['fls/01_1.nii', 'fls/02_1.nii', 'fls/03_1.nii', 'fls/04_1.nii']
What I'm trying to achieve is to move the list into MATLAB - I need to convert it into a 4x1 cell (the outcome in MATLAB would look like that):
4×1 cell array
{'fls/01_1.nii'}
{'fls/02_1.nii'}
{'fls/03_1.nii'}
{'fls/04_1.nii'}
What I've done so far in Python:
import matlab.engine, matlab
eng = matlab.engine.start_matlab()
#first option
eng.workspace['matlab_list'] = lst
#second option
eng.workspace['matlab_list'] = eng.cellstr(lst)
#third option
eng.workspace['matlab_list'] = eng.reshape(eng.cellstr(lst), 4, 1)
#fourth option
eng.workspace['matlab_list'] = lst
eng.workspace['matlab_list'] = eng.eval("cellstr(lst)")
The code above should pretty much do the trick - my MATLAB engine even tells me that I'm dealing with a cell:
In[73]: eng.eval('class(matlab_list)')
Out[73]: 'cell'
Sadly, whenever I try to use that variable in a function from SPM (MATLAB based software package used in analysis of brain imaging data sequences), I'm getting an error:
Item 'Time 1 Volumes', field 'val': Value must be either empty, a cellstr or a cfg_dep object.
Here's what shows up in MATLAB_R2018a if I save the variable into a .mat file and try loading it there:
matlab_list =
4×12 char array
'fls/01_1.nii'
'fls/02_1.nii'
'fls/03_1.nii'
'fls/04_1.nii'
>> cellstr(matlab_list)
ans =
4×1 cell array
{'fls/01_1.nii'}
{'fls/02_1.nii'}
{'fls/03_1.nii'}
{'fls/04_1.nii'}
Using the cellstr() function does the trick in MATLAB, but now I'm pretty keen on finding a way to change the char array into a cell array using Python MATLAB engine - so far no luck and I'm out of ideas at the moment.
I would be very grateful for any help or suggestions on fixing this particular issue.
Best Regards,
MG
0 comentarios
Respuestas (1)
Ver también
Categorías
Más información sobre Call Python from MATLAB en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!