How to pass a writable array to a python function?
14 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(1 个)
Vidhi Agarwal
2024-2-15
编辑:Vidhi Agarwal
2024-2-15
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 "save" you can refer to : https://in.mathworks.com/help/matlab/ref/save.html?searchHighlight=save&s_tid=srchtitle_support_results_1_save#:~:text=save(filename)%20saves%20all%20variables%20from%20the%20current%20workspace%20in%20a%20binary%20MATLAB%C2%AE%20file%20(MAT%2Dfile)%20named%20filename.%20If%20filename%20exists%2C%20save%20overwrites%20the%20file.
For a detailed understanding, of "loadmat" please consult the SciPy documentation: scipy.io.loadmat — SciPy v1.12.0 Manual
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!