Use Python Numeric Variables in MATLAB
This example shows how to use Python® numeric types in MATLAB®.
Use Python Numeric Types in MATLAB
当调用一个Python函数numeric input argument, MATLAB converts double values into types that best represent the data to the Python language. For example, to call trigonometry functions in the Pythonmath
module, pass a MATLAB double value.
pynum = py.math.radians(90)
pynum = 1.5708
For functions that return Pythonfloat
types, MATLAB automatically converts this type to double.
class(pynum)
ans = 'double'
For Python functions returning integer types, MATLAB automatically converts this type toint64
. For example, thebit_length
function returns the number of bits necessary to represent an integer in binary as anint
value.
py.int(intmax).bit_length
ans =int6431
Call Python Methods with Numericiterable
Arguments
The Pythonmath.fsum
function sums floating-point values in aniterable
input argument. You can pass a MATLAB vector to this function. For example, open the MATLABpatients.mat
data file and read the numeric arrayHeight
.
loadpatients.matclass(Height)
ans = 'double'
size(Height)
ans =1×2100 1
When you pass this argument to Python, MATLAB automatically converts the numeric values to Python numeric values and Python iterates over the vector values.
py.math.fsum(Height)
ans = 6707
Use Pythonarray
Types in MATLAB
Suppose that you have a Python function that returns the following Pythonarray.array
of type double.
P = py.array.array('d', 1:5)
P = Python array with properties: itemsize: 8 typecode: [1×1 py.str] array('d', [1.0, 2.0, 3.0, 4.0, 5.0])
To passP
to the MATLAB functionsum
, convertP
to a MATLAB array of type double.
sum(double(P))
ans = 15
Use Python Integerarray
Types in MATLAB
Suppose that you have this Python array. Call the Pythonreverse
function on the array, then convert the result to a MATLAB array.
arr = py.array.array('i',[int32(5),int32(1),int32(-5)])
arr = Python array with properties: itemsize: 4 typecode: [1×1 py.str] array('i', [5, 1, -5])
arr.reverse A = int32(arr)
A =1×3 int32 row vector-5 1 5
Default Numeric Types
By default, a number in MATLAB is adouble
type. By default, a number (without a fractional part) in Python is an integer type. This difference can cause confusion when passing numbers to Python functions.
For example, when you pass these MATLAB numbers to the Pythondatetime
function, Python reads them asfloat
types and displays an error:
d = py.datetime.date(2014,12,31)
Python Error: TypeError: integer argument expected, got float
To correct the error, explicitly convert each number to an integer type:
d = py.datetime.date (int32 (2014), int32 (12), int32 (31))
d = Python date with properties: day: 31 month: 12 year: 2014 2014-12-31
Why Do I See Properties When I Display a Number?
MATLAB displays all Python types as objects, which includes a list of object properties. For numeric types, MATLAB displays the expected output value on the last line.
py.int(5)
ans = Python int with properties: denominator: 1 imag: 0 numerator: 5 real: 5 5