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

6 vues (au cours des 30 derniers jours)
Marvin C
Marvin C le 25 Juil 2022
Modifié(e) : Walter Roberson le 22 Août 2024
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.

Réponses (1)

arushi
arushi le 22 Août 2024
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.

Catégories

En savoir plus sur Call MATLAB from Python dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by