### 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:**

- Matlab version 2014b or above.
- Python 2.7, 3.4 or 3.5 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:**

- Run MATLAB and in command window enter the following command
`>> matlabroot ans = 'C:\Program Files\MATLAB\R2017a'`

C:\Program Files\MATLAB\R2017a' is the default matlabroot folder on Windows. This may be different on your system.
- 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. - After successful install you can run the following commands to test if you can run MATLAB code in python

You should be able to see a graphics figure when you run the last command above.`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`

## Comments

## Post a Comment