-1

I would really appreciate some help with running code written in Python 3 from Matlab. My Python code loads various libraries and uses them to perform numerical integration of a differential equation (for the numpy vector: e_array ). The Python code which I would like to call from Matlab is the following:

from numba import jit
from scipy.integrate import quad
import numpy as np

@jit(nopython = True)  
def integrand1(x,e,delta,r):
    return (-2*np.sqrt(e*r)/np.pi)*(x/np.sqrt(1-x**2))/(1+(delta+2*x*np.sqrt(e*r))**2)

@jit(nopython = True) 
def f1(e,delta,r):
    return quad(integrand1, -1, 1, args=(e,delta,r))[0]

@jit(nopython = True)  
def runge1(e,dtau,delta,r):
    k1 = f1(e,delta,r)
    k2 = f1((e+k1*dtau/2),delta,r)
    k3 = f1((e+k2*dtau/2),delta,r)
    k4 = f1((e+k3*dtau),delta,r)
    return e + (dtau/6)*(k1+2*k2+2*k3+k4)

time_steps = 60
e = 10
dtau=1
r=1
delta=-1

e_array = np.zeros(time_steps)
time = np.zeros(time_steps)
for i in range(time_steps):
    e_array[i] = e
    time[i] = i*dtau       
    e = runge1(e,dtau,delta,r)

Ideally, I would like to be able to call this Python code (pythoncode.py) in Matlab as if it were a Matlab function and feed it the parameters: time_steps, e, dtau, r and delta. I would be very happy with a solution which looks like this:

e_array = pythoncode.py(time_steps = 60, e = 10, dtau = 1, r = 1, delta = -1)

where pythoncode.py is treated as a Matlab function which takes said parameters, feeds them into the Python code and returns the Matlab vector e_array.

I want to point out that there are several additional Python codes that I'd like to be able to call from Matlab and I'm hope to get an idea of how to do this from your answers regarding this specific Python code. A related question involves the Python libraries which I use in the Python code: Is there a way to "compile" the Python code such that I can call it in Matlab without installing the libraries it uses (f.e the numba library) on the computer running the Matlab code?

Thanks very much for helping, Asaf

Asaf M
  • 21
  • 2
  • 1
    Did you read [the documentation](https://www.mathworks.com/help/matlab/matlab_external/call-python-from-matlab.html)? – Suever Feb 21 '17 at 14:10
  • @Suever I've definitely tried but I wasn't able understand how to use it. – Asaf M Feb 21 '17 at 14:15
  • All information on the Python interface is [here](https://www.mathworks.com/help/matlab/call-python-libraries.html). Can you show us the code you have tried that doesn't work? – Suever Feb 21 '17 at 14:16
  • @Suever I haven't gotten far enough to trying to actually implement an actual solution since I'm honestly not sure where to begin. I was hoping that the solution to my specific code would be simple and that I could learn from it. – Asaf M Feb 21 '17 at 14:19

1 Answers1

0

You'll probably need to shell escape out of Matlab to invoke python -- prefix the command you'd run on the shell with !.

Matlab Shell Escape Functions suggests saving a mat file and then opening it in your python code -- see Read .mat files in Python .

In terms of compiling the python, you could take a look at How to compile a Python file and see if that helps you.

Community
  • 1
  • 1
Dragon
  • 1,743
  • 1
  • 16
  • 29
  • Thanks @Dragon by I'm getting a "The page you were looking for does not exist." message for you first link. Also, I'm very new to programming and don't know shell escape means but I want to point out that I'm looking to run the Python code in Matlab and not the other way around. Thanks agian – Asaf M Feb 21 '17 at 14:24
  • Fixed the URL, thanks! If you want to run Python from Matlab, Matlab must pass control and the data the function requires to Python. The first of these is done by the shell escape [essentially: "stop what you're doing and run this other command"], the appproach to the second is for matlab to save the data, invoke python then python to load the data, do some calcs, save some new data, and then matlab to load the data back again. There is definitely not a pure python solution to this. – Dragon Feb 21 '17 at 15:09
  • Is there any chance you could write the lines of code one would have to add to both the Python and Matlab codes to make it work for my example or is it involved/heavy? Thanks for your help so-far anyway! – Asaf M Feb 21 '17 at 17:03