How do I convert matlab.double arrays to numpy arrays when using MATLAB Engine for Python?
45 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2022 年 11 月 14 日
回答済み: MathWorks Support Team
2022 年 11 月 15 日
When working in Python and using the MATLAB Engine API for Python, how can I efficiently convert the matlab.double arrays to numpy arrays?
採用された回答
MathWorks Support Team
2022 年 11 月 14 日
There are a couple of ways to accomplish this. One method is to use numpy's built-in method, 'asarray':
Start by loading your matlab.double array:
myData = eng.eval("load('{}','cluster_class','par')".format('sampleData.mat'))
With MATLAB R2022a and later, you can convert matlab.double directly to a numpy array:
a = np.array(myData['cluster_class'])
To add additional specification, use MATLAB engine's functions to convert to a Python array with 'noncomplex()', then to a numpy array:
a = np.array(myData['cluster_class'].noncomplex().toarray(), 'int')
We use the 'noncomplex()' call to retrieve the data in 1D format (provided it's noncomplex; for complex data, we can use 'real()' and 'imag()' calls). We also offer the 'toarray()' call to convert a 1D matlab.double into a Python array.array object.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Call MATLAB from Python についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!