How to pass a writable array to a python function?

13 Ansichten (letzte 30 Tage)
Søren Gregersen
Søren Gregersen am 17 Dez. 2021
Bearbeitet: Vidhi Agarwal am 15 Feb. 2024
Say you have the python function:
# myfunc.py
import numpy as np
def myfunc(array):
array = np.asanyarray(array)
array[2] = 1
return array
and you wish to use it using the matlab code:
% myscript_with_error.m
array = zeros(5);
changed_array = py.myfunc.myfunc(array); % generates an ERROR!
When you run it you get:
>> myscript_with_error
Error using myfunc (line 5)
Python Error: ValueError: assignment destination is read-only
Python function 'myfunc' might not be able to accept at least one input argument at position 1. The function may require a specific data type that you can construct from the MATLAB array. For more information, see the documentation for Python function 'myfunc' and working with Python arrays.
The python module complains that the array is not writable. In other words, matlab passes an immutable array to python.
The immediate fix is easy: just change the matlab code to:
% myscript_no_error.m
array = zeros(5);
changed_array = py.myfunc.myfunc(py.numpy.array(array)); % Makes a COPY!
And when you run it:
>> myscript_no_error
>> changed_array
changed_array =
Python ndarray:
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0
0 0 0 0 0
Use details function to view the properties of the Python object.
Use double function to convert to a MATLAB array.
Yay! However, this creates a copy as seen by looking at the array again:
>> array
array =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
To summarize: How to pass a writable array from matlab in order to avoid unnecessary copies?

Antworten (1)

Vidhi Agarwal
Vidhi Agarwal am 15 Feb. 2024
Bearbeitet: Vidhi Agarwal am 15 Feb. 2024
Hi Søren,
It is acknowledged that transferring an array from the MATLAB workspace to a Python script necessitates copying the array to render it compatible for use within the Python environment.
Python's buffer protocol allows direct access to MATLAB arrays within the same process, eliminating the need for data copying. While many Python functions can work with MATLAB arrays as is, some require conversion to types like numpy.ndarray, especially if data modification is involved. To avoid errors, ensure MATLAB arrays are converted to the appropriate Python type before use. Refer the documentation for further clarity.
A workaround for this is to transfer array data from MATLAB to Python is by saving the array to a .mat file using MATLAB's "save" function. Subsequently, this file can be loaded into Python using the "loadmat" function from the scipy.io module.
For a detailed understanding, of "loadmat" please consult the SciPy documentation: scipy.io.loadmat — SciPy v1.12.0 Manual

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by