This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Default Numeric Types in MATLAB and Python

MATLAB® stores all numeric values as double-precision floating point numbers by default. This differs from Python®, which stores some numbers as integers by default. Because of this difference, you might pass integers as input arguments to MATLAB functions that expect double-precision numbers.

Consider these variable assignments in MATLAB:

x = 4;
y = 4.0;

Both x and y are of data type double. Now consider the same assignments in Python:

x = 4
y = 4.0

x and y are of different numeric data types.

<type 'int'>
<type 'float'>

Most MATLAB functions take numeric input arguments of data type double. The best practice is to ensure that numbers you pass as input arguments to MATLAB functions are of Python data type float, not Python data type int. You can ensure that Python variables are floating point numbers if you:

  • Make literals floating point numbers. For example, type 4.0 instead of 4.

  • Convert to data type float. For example, x = float(4) casts the number to data type float.

  • Create a matlab.double array from a number or sequence. For example, x = matlab.double([1,2,3,4,5]) creates an array of MATLAB data type double from a list of Python integers.

When you pass an integer to a MATLAB function that takes an input argument of data type double, the engine raises an error. See MatlabExecutionError: Undefined Function for an example.

When you call a MATLAB function that does take integers as numeric input arguments, you can pass input arguments of Python data type int to the function.

Was this topic helpful?