MATLAB: How to convert Python list to column vector

column vectorfitdistmatlab enginepythonpython list

I'm using the fitdist method from MATLAB in Python where x is:
I tried different approaches to work on this and they all give the same error:
eng.fitdist(eng.cell2mat(list(x)), 'stable')
eng.fitdist(matlab.double(list(x)), 'stable')
eng.fitdist(list(x), 'stable')
All of these give me this error:
MatlabExecutionError:
File C:\Program Files\MATLAB\R2020b\toolbox\stats\stats\fitdist.m, line 126, in fitdist X must be a numeric column vector.
Any idea how to get out of it? How do I convert my list to a column vector that works with MATLAB?
I am using MATLAB R2020b

Best Answer

For anyone running into this, below is what resolved it for me:
"You may use the size argument of matlab.double for creating a column vector.
Column vector in MATLAB is equivalent to 2D matrix with second dimension size equals 1.
For example: A Matrix with size [5, 1] is a column vector with 5 columns.
According to the documentation, MATLAB Arrays as Python Variables:
matlab.double has an optional size argument:
matlab.double(initializer=None, size=None, is_complex=False)
You can set size argument to (x.size, 1) for creating a column vector.
The following syntax works (assuming x is a NumPy array):
eng.fitdist(matlab.double(list(x), (x.size, 1)), 'stable')
The following syntax also works:
eng.fitdist(matlab.double(list(x), (len(list(x)), 1)), 'stable')
The following code was used for testing:
import numpy as np
import matlab
import matlab.engine
eng = matlab.engine.start_matlab()
x = np.array([176.0, 163.0, 131.0, 133.0, 119.0, 142.0, 142.0, 180.0, 183.0, 132.0, 128.0, 137.0, 174.0])
eng.fitdist(matlab.double(list(x), (x.size, 1)), 'stable')
"
Source:
https://stackoverflow.com/questions/66247949/how-can-i-convert-python-list-to-column-vector