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

12 次查看(过去 30 天)
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