How to run MATLAB code from Python

Python is a free open source programming language and can be used to solve a number of problems from research purposes to web development and big data analysis. MATLAB provides a list of professionally developed specialized toolboxes for a number of problems. It is sometimes required to use MATLAB code in your python script. I have listed below a set of instructions to run MATLAB code in python.
NOTE: To run MATLAB code using this method you will still need MATLAB licence.


  1. Matlab version 2014b or above.
  2. Python 2.7, 3.4 or 3.5
  3. My configuration is MATLAB 2017a and Python 3.5 Anaconda x64:

Installation Instructions:

  1. Run MATLAB and in command window enter the following command
    >> matlabroot 
    ans = 
       'C:\Program Files\MATLAB\R2017a'
  2. C:\Program Files\MATLAB\R2017a' is the default matlabroot folder on Windows. This may be different on your system.
  3. Open Windows command window (or launch terminal on linux) in matlabroot folder and enter the following commands.
    C:\Program Files\MATLAB\R2017a> cd extern\engines\python
    C:\Program Files\MATLAB\R2017a\extern\engines\python> python install 
    If you do not have access permissions then (a) you can run command window as administrator (or as root in linux) and run the above commands or (b) you can install MATLAB API in non default locations by using the following command
    C:\Program Files\MATLAB\R2017a\extern\engines\python> python build --build-base="builddir" install --prefix="installdir" 
    where "builddir" and "installdir" are non-default build and installation folders.
  4. After successful install you can run the following commands to test if you can run MATLAB code in python
     C:\> python
     >>> import matlab.engine
     >>> eng = matlab.engine.start_matlab()
     >>> tf = eng.isprime(37)
     >>> print(tf)
     >>> eng.eval("figure", nargout=0) # To show graphics figure
    You should be able to see a graphics figure when you run the last command above.


Popular posts from this blog

Build caffe, pycaffe and matcaffe on Windows

Running Tensorboard on Windows

Build TensorFlow on Windows