## 4.7 Using MATLAB^{TM}

### 4.7.1 Introduction to MATLAB^{TM}

MATLAB^{TM} is a numerical computing environment and an interpreted programming language.
MATLAB^{TM} allows easy matrix manipulation, plotting of functions and data, implementation of algorithms and creation of user interfaces.
You can get more information on the official MathWorks web site.
MATLAB^{TM} is widely used in robotics in particular for its *Image Processing, Neural Networks* and *Genetics Algorithms* toolboxes.
Webots allows to directly use MATLAB^{TM} scripts as robot controller programs for your simulations.
Using the MATLAB^{TM} interface, it becomes easy to visualize controller or supervisor data, for example, processed images, sensor readings, the performance of an optimization algorithm, etc.,
while the simulation is running.
In addition, it becomes possible to reuse your existing MATLAB^{TM} code directly in Webots.

### 4.7.2 How to run the Examples?

If MATLAB^{TM} is already installed, you can directly launch one of the MATLAB^{TM} examples.
For doing that, start Webots and open the world file `projects/languages/matlab/worlds/e-puck_matlab.wbt` or the world file
`projects/robots/nao/worlds/nao2_matlab.wbt` in your Webots installation directory.
Webots automatically starts MATLAB^{TM} when it detects an m-file in a controller directory.
Note that the m-file must be named after its directory in order to be identified as a controller file by Webots.
So, for example, if the directory is named `my_controller`, then the controller m-file must be named `my_controller/my_controller.m`.

No special initialization code is necessary in the controller m-file.
In fact Webots calls an intermediate `launcher.m` file that sets up the Webots controller environment and then calls the controller m-file.
In particular the `launcher.m` file loads the library for communicating with Webots and adds the path to API m-files.
The MATLAB^{TM} API m-files are located in the `lib/matlab` directory of Webots distribution.
These are readable source files; please report any problem, or possible improvement about these files.

### 4.7.3 MATLAB^{TM} Installation

In order to use MATLAB^{TM} controllers in Webots, the MATLAB^{TM} software must be installed (The MathWorks^{TM} license required).

Webots must be able to access the `matlab` executable (usually a script) in order to run controller m-files.
Webots looks for the *matlab* executable in every directory of your *PATH* (or *Path* on Windows) environment variable.
Note that this is similar to calling `matlab` from a terminal (or *Command Prompt* on Windows),
therefore, if MATLAB^{TM} can be started from a terminal then it can also be started from Webots.

On Windows, the MATLAB^{TM} installer will normally add MATLAB^{TM}'s bin directories to your *Path* environment variable,
so usually Webots will be able to locate MATLAB^{TM} after a standard installation.
However, in case it does not work, please make sure that your *Path* contains this directory
(or something slightly different, according to your MATLAB^{TM} version):

`Path=C:\Program Files\MATLAB\R2009b\bin` |

On Linux, the MATLAB^{TM} installer does normally suggest to add a symlink to the `matlab` startup script in the `/usr/local/bin` directory.
This is a good option to make `matlab` globally accessible.
Otherwise you can create the link at anytime afterwards with this shell command (please change according to your actual MATLAB^{TM} installation directory and version):

`$ sudo ln -s /usr/local/MATLAB/R2014a/bin/matlab /usr/local/bin/matlab` |

`matlab`startup script then you should add a symlink in

`/usr/bin`:

`$ sudo ln -s /Applications/MATLAB_R2014a.app/bin/matlab /usr/bin/matlab` |

### 4.7.4 Compatibility Issues

We recommend to use the latest Matlab version on an up-to-date operating system.

Note that 32-bit versions of Webots are not compatible with 64-bit versions of MATLAB^{TM} and vice-versa.
On Windows, Webots comes only in 32-bit flavour and therefore it can only inter-operate with a-32 bit version of MATLAB^{TM}.
So for example on a 64-bit Windows, you will need to install a 32-bit version of MATLAB^{TM} to inter-operate with Webots.
On Linux, the 32-bit version of Webots can only inter-operate with a 32-bit version of MATLAB^{TM} and
the 64-bit version of Webots can only inter-operate with a 64-bit version of MATLAB^{TM}.
On Mac OS X, there is only one version of Webots but that version is compatible with both 32-bit and 64-bit installations of MATLAB^{TM}.

On some platform the MATLAB^{TM} interface needs `perl` and `gcc` to be installed separately.
These tools are required because MATLAB^{TM}'s `loadlibrary()` function will need to recompile Webots header files on the fly.
According to MATLAB^{TM}'s documentation this will be the case on 64-bit systems, and hence we advice 64-bit Webots users (on Linux) to make sure that these packages are installed on their systems.

On some Mac OS X systems the MATLAB^{TM} interface will work only if you install the Xcode development environment, because `gcc` is required.
An error message like this one, is a symptom of the above described problem:

`error using ==> calllib` |