Converting matlab.double to python compatible format
46 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MATLAB Input Argument Type —
1-by-N Vector
double (real)
Gets converted to:
Resulting Python Type
array.array('d')
Consequently, I thought this python code would work:
import matlab.engine
import array
me = matlab.engine.start_matlab()
array.array('d', me.rand(1, 2))
However, this returns:
TypeError: must be real number, not matlab.double
What is the most pythonic way to convert a standard matlab double array into a python fundamental data type? (e.g. list or array.array)
0 Kommentare
Antworten (1)
Selena Mastrodonato
am 4 Mai 2023
This syntax py.array.array('d', rand(1,2)) should work.
py.array.array('d', rand(1,2))
Or if you want a Python list: py.list(rand(1,2))
py.list(rand(1,2))
3 Kommentare
Selena Mastrodonato
am 5 Mai 2023
Hi Lyle, thank you for providing more informations.
You could try this command: me.rand(1, 2)[0].toarray(), you'll obtain array('d', [0.8147236863931789, 0.9057919370756192]).
Instead, if you want a python list you could use numpy, so try this: numpy.array(me.rand(1, 2)[0].toarray()) and you'll obtain [0.81472369 0.90579194].
If you have a multidimensional matrix, try to iterate over it to format data as you wish.
Siehe auch
Kategorien
Mehr zu Call Python from MATLAB 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!