How to convert data types in Python to mlarray?

4 views (last 30 days)
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?

Answers (1)

rakshit gupta
rakshit gupta on 6 Jun 2023
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)

Community Treasure Hunt

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

Start Hunting!