How to convert data types in Python to mlarray?

4 Ansichten (letzte 30 Tage)
slevin Lee
slevin Lee am 16 Nov. 2022
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?

Antworten (1)

rakshit gupta
rakshit gupta am 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)

Kategorien

Mehr zu Call MATLAB from Python finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by