How do I pass complex numbers to MATLAB Engine? (Python)

조회 수: 6 (최근 30일)
Marvin C
Marvin C 2022년 7월 25일
편집: Walter Roberson 2024년 8월 22일
In Python I'm doing
import matlab.engine
eng = matlab.engine.start_matlab()
ret = eng.circuit_values(1, 1, 1, 120, -60-103.92j, -60 + 103.92j)
print(complex(ret))
But I get the error
Error using *
Complex integer arithmetic is not supported.
Is there anyway to work around this?
the .m file looks like this
function [VnN] = circuit_values(zA, zB, zC, VAN, VBN, VCN)
VnN = ((1/zA)*VAN + (1/zB)*VBN + (1/zC)*VCN)/((1/zA)+(1/zB)+(1/zC));
end
Any help is appreciated.

답변 (1개)

arushi
arushi 2024년 8월 22일
Hi Marvin C
To pass complex numbers to MATLAB from python, you can utilize the 'matlab.double' function and set the 'is_complex' parameter to 'True'.
This will allow you to create an array of complex numbers in Python and then pass it to MATLAB using the MATLAB engine.
Here's a sample Python script that demonstrates this process:
import matlab.engine
eng = matlab.engine.start_matlab()
a = matlab.double([[1.1+2.4j, 3+4j],[5.3,6.7]], is_complex=True)
ret = eng.example(a)
In the above code snippet, the function ‘example’ resides in the ‘.m’ file and this function takes the ‘a’ as an input, produces the output and returns it back to the python script.
Hope this helps.

카테고리

Help CenterFile Exchange에서 Call MATLAB from Python에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by