How to convert data types in Python to mlarray?

4 次查看(过去 30 天)
import matlab.engine
eng = matlab.engine.start_matlab()
# mlarray to ndarray: np.asarray(x._data, dtype=dtype)
c = eng.magic(5)
print(type(c))
d = np.asarray(c)
print(type(d))
# ndarray to mlarray:?????
a = np.random.random(10)
a1 = a.tolist()
a2 = eng.cell(a1)
a3 = eng.cellfun(@double,a2,'UniformOutput',false)
print(type(a1))
print(type(a2))
print(type(a3))
SyntaxError: invalid syntax
How to convert data types in Python to mlarray?

回答(1 个)

rakshit gupta
rakshit gupta 2023-6-6
To convert a ndarray to a mlarray in Python using MATLAB Engine, you can use the 'matlab.double()' function.
Here's an example of how you can convert an ndarray to mlarray:
import matlab.engine
import numpy as np
eng = matlab.engine.start_matlab()
# create an ndarray
a = np.random.random(10)
# convert ndarray to mlarray
mla = matlab.double(a.tolist())
# verify that the mlarray was created
print(type(mla))
# pass the mlarray to a MATLAB function, for example, to calculate the sum
result = eng.sum(mla)
# print the result
print(result)

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from Python 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by