Pass matlab array to python
When calling a Python® function, MATLAB® converts MATLAB data into types that best represent the data to the Python language. Show Pass Scalar Values to Python
Pass Vectors to Python
Pass Matrices and Multidimensional Arrays to PythonThe Python language provides a protocol for accessing memory buffers like the data stored in a MATLAB array. MATLAB implements this Python buffer protocol for MATLAB arrays so that you can read MATLAB arrays directly from Python code, running in the same process as MATLAB, without copying data. Many Python functions directly use the MATLAB array from Python without converting it to a native Python type. Some functions might require a specific type, such as p = py.numpy.array(magic(3)) p = Python ndarray: 8 1 6 3 5 7 4 9 2 Use details function to view the properties of the Python object. Use double function to convert to a MATLAB array. MATLAB sparse arrays are not supported in Python. See Unsupported MATLAB Types. Troubleshooting Argument ErrorsIf a Python function expects a specific Python multidimensional array type such as
For example, suppose that the following code returns an error. a = [1 2; 3 4]; py.pyfunc(a) If the documentation of py.pyfunc(numpy.ndarray(a)) If the error persists, then determine the root cause by checking for additional information in the Python exception. Automatically Convert Python Types to MATLAB TypesMATLAB automatically converts these data types returned from Python functions into MATLAB types. To convert other types, see Explicitly Convert Python Types to MATLAB Types.
Explicitly Convert Python Types to MATLAB TypesIf the output of a Python function implements the Python buffer protocol, such as
Use these MATLAB functions to convert Python data types to MATLAB types.
For example, a Python function returns this array p = Python ndarray: 8 1 6 3 5 7 4 9 2 Use details function to view the properties of the Python object. Use double function to convert to a MATLAB array. You can convert it to a MATLAB matrix P = 3×3 8 1 6 3 5 7 4 9 2 If you need specific information about the Python properties of py.numpy.ndarray handle with properties: T: [1×1 py.numpy.ndarray] base: [1×1 py.NoneType] ctypes: [1×1 py.numpy.core._internal._ctypes] data: [1×3 py.memoryview] dtype: [1×1 py.numpy.dtype[float64]] flags: [1×1 py.numpy.flagsobj] flat: [1×1 py.numpy.flatiter] imag: [1×1 py.numpy.ndarray] itemsize: [1×1 py.int] nbytes: [1×1 py.int] ndim: [1×1 py.int] real: [1×1 py.numpy.ndarray] shape: [1×2 py.tuple] size: [1×1 py.int] strides: [1×2 py.tuple] Methods, Events, Superclasses If the Python module provides content in its Unsupported MATLAB TypesThese MATLAB types are not supported in Python.
Related Topics
|