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.

Requirements:

  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: https://repo.continuum.io/archive/Anaconda3-4.2.0-Windows-x86_64.exe

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 setup.py 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 setup.py 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)
     True
     >>> 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.

3 comments:

  1. Your new valuable key points simply much a person like me and extremely more to my office workers. With thanks; from every one of us.
    Best python training Institute in marathahalli

    ReplyDelete
  2. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.

    Python Training in Chennai | Python Training Institutes in Chennai

    ReplyDelete
  3. I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation.
    Python Training in Bangalore

    ReplyDelete

How to run R code in Python

R is freely available language and computational tool which is very popular among the statisticians and bioinformatics community. In this p...