how can run a python function in matlab, with a matlab function inside the python function
6 views (last 30 days)
Show older comments
For example, I have define a matlab function
f_mat(a,b)
, and a python function
f_py(f_mat',a',b')
It will run in matlab as:
py.f_py(f_mat',a',b')
Can I pass the matlab function f_mat(a,b) and parameters a,b directly to this python function? I guess not, inside the python function, all the function or parameters should be in python form. how to convert the matlab function that python can use?
1 Comment
David Hill
on 9 Nov 2021
You could make a matlab function that uses python code. What is the python function doing?
Answers (1)
Tejas
on 19 Feb 2024
Hello Peng Wu,
Passing a MATLAB function as an argument to a Python function can be somewhat complex due to the different execution environments of MATLAB and Python. However, I have a practical workaround to propose. Begin by saving the MATLAB function that you wish to execute from the Python script into a separate file.
Once you have your MATLAB function in a standalone file, you can access it from your Python script using the 'MATLAB Engine API'. This API bridges the gap between the two languages, allowing your Python code to interact directly with the MATLAB file.
After setting up the API, encapsulate your Python code in a separate file. This organization will enable you to invoke the Python file from MATLAB using the ‘pyrunfile’ function.
The example I have provided consists of three files: 'product.m', which is the MATLAB file to be called from the Python script; a Python file named 'python_func.py'; and a main MATLAB file named 'Main.m', from which the Python file will be executed. I have changed the structure of python file to only take two inputs : ‘a’ & ‘b’.
Make sure that all these files are either located in the same directory or have been added to the MATLAB path. Below is the code for each file, along with a screenshot showing the output.
product.m
function num = product(a,b)
num = a * b ;
end
python_func.py
import matlab.engine
def python_function(a,b):
Engine = matlab.engine.start_matlab()
matlab_a = matlab.double([a])
matlab_b = matlab.double([b])
result = Engine.product(matlab_a,matlab_b)
Engine.quit()
return result
z=python_function(a,b)
Main.m
res= pyrunfile("python_func.py","z",a=3,b=5);
disp(res);
ScreenShot of Output
This solution is tested on MATLAB R2023b and Python 3.11.8 .
For additional details on the ‘pyrunfile’ function and its usage, please consult the MATLAB documentation : https://www.mathworks.com/help/matlab/ref/pyrunfile.html#:~:text=A%27%2C%20%27new%27%2C%20%27list%27%5D-,Pass%20MATLAB%20Arguments%20to%20Python%20Script,%22addac.py%22%2C%22z%22%2Cx%3D3%2Cy%3D2),-res%20%3D%205.
For more information on how to use the 'MATLAB Engine API', please refer to this MATLAB documentation : https://www.mathworks.com/help/matlab/matlab_external/call-matlab-functions-from-python.html#:~:text=0)%0Aeng.myFnc()-,Return%20Output%20Argument%20from%20MATLAB%20Function,engine%0Aeng%20%3D%20matlab.engine.start_matlab()%0Atf%20%3D%20eng.isprime(37)%0Aprint(tf),-True.
For steps to install 'MATLAB Engine API' , please refer to this documentation : https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html#:~:text=not%20already%20there.-,Install%20Engine%20API,commands%2C%20see%20Python%20Setup%20Script%20to%20Install%20MATLAB%20Engine%20API.,-Start%20MATLAB%20Engine.
Hope it helps!
0 Comments
See Also
Categories
Find more on Call MATLAB from Python in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!