Use Python Numeric Variables in MATLAB
This example shows how to use Python® numeric types in MATLAB®.
Use Python Numeric Types in MATLAB
By default, a number in MATLAB has a double-precision type. However, in Python, a number without a fractional part has, by default, an integer type. To resolve conversion errors when calling a Python function that takes a 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 Python math
module, pass a MATLAB double
value.
pynum = py.math.radians(90)
pynum = 1.5708
For functions that return a Python float
type, MATLAB automatically converts the type to double
.
class(pynum)
ans = 'double'
For functions that return a Python int
type, MATLAB automatically converts the type to int
. For example, the bit_length
function returns the number of bits necessary to represent an integer in binary as an int
value.
py.int(intmax).bit_length
ans = Python int with properties: denominator: [1×1 py.int] imag: [1×1 py.int] numerator: [1×1 py.int] real: [1×1 py.int] 31
MATLAB also uses Python type hints when converting MATLAB double
values to Python int
before passing arguments to a Python function. (since R2024b)
For example, the Python date
constructor requires integer inputs. When you pass MATLAB numbers to the Python date
constructor in the datetime
module, MATLAB first converts the double
values to integers.
d = py.datetime.date(2014,12,31)
d = Python date with properties: day: [1×1 py.int] month: [1×1 py.int] year: [1×1 py.int] 2014-12-31
The conversion from double
to int
is also supported for user-authored functions that provide type hint information. However, the use of type hint information in MATLAB is limited. For example, modules such as the Python typing
module are not supported. MATLAB also does not support type hints from stub modules or modules containing stubs. Further, MATLAB converts double
to int
only if there is no precision loss.
Call Python Methods with Numeric iterable
Arguments
The Python math.fsum
function sums floating-point values in an iterable
input argument. You can pass a MATLAB vector to this function. For example, open the MATLAB patients.mat
data file and read the numeric array Height
.
load patients.mat
class(Height)
ans = 'double'
size(Height)
ans = 1×2
100 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 Python array
Types in MATLAB
Suppose that you have a Python function that returns the following Python array.array
of type double.
P = py.array.array('d', 1:5)
P = Python array: 1 2 3 4 5 Use details function to view the properties of the Python object. Use double function to convert to a MATLAB array.
To pass P
to the MATLAB function sum
, convert P
to a MATLAB array of type double
.
sum(double(P))
ans = 15
Use Python Integer array
Types in MATLAB
Suppose that you have this Python array. Call the Python reverse
function on the array, and then convert the result to a MATLAB array.
arr = py.array.array('i',[int32(5),int32(1),int32(-5)])
arr = Python array: 5 1 -5 Use details function to view the properties of the Python object. Use int32 function to convert to a MATLAB array.
arr.reverse A = int32(arr)
A = 1×3 int32 row vector
-5 1 5
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×1 py.int] imag: [1×1 py.int] numerator: [1×1 py.int] real: [1×1 py.int] 5