4.7 Using MATLABTM
4.7.1 Introduction to MATLABTM
MATLABTM is a numerical computing environment and an interpreted programming language. MATLABTM 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. MATLABTM is widely used in robotics in particular for its Image Processing, Neural Networks and Genetics Algorithms toolboxes. Webots allows to directly use MATLABTM scripts as robot controller programs for your simulations. Using the MATLABTM 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 MATLABTM code directly in Webots.
4.7.2 How to run the Examples?
If MATLABTM is already installed, you can directly launch one of the MATLABTM examples. For doing that, start Webots and open the world file WEBOTS_MODULES_PATH/projects/languages/matlab/worlds/e-puck_matlab.wbt or the world file WEBOTS_MODULES_PATH/projects/robots/aldebaran/worlds/nao2_matlab.wbt in your Webots installation directory. Webots automatically starts MATLABTM 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 MATLABTM 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 MATLABTM Installation
In order to use MATLABTM controllers in Webots, the MATLABTM software must be installed (The MathWorksTM 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 MATLABTM can be started from a terminal then it can also be started from Webots.
On Windows, the MATLABTM installer will normally add MATLABTM's bin directories to your Path environment variable, so usually Webots will be able to locate MATLABTM 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 MATLABTM version):
On Linux, the MATLABTM 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 MATLABTM installation directory and version):
4.7.4 Display information to Webots console
On Linux and Mac OS X, the Matlab output is redirected as is to the Webots console. This means you can use all the Matlab display features (disp(), display(), omitting the semicolon character at the end of a statement, etc.).
On Windows, the Matlab output is not redirected to the Webots console. The wb_console_print(text, stream) function should be used to display some text in the Webots console. The second argument (stream) can be either WB_STDOUT or WB_STDERR depending on which stream you would like to write.
In order to create a cross-platform controller, it is recommended to use the wb_console_print(text, stream) on every OS.
4.7.5 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 MATLABTM 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 MATLABTM. So for example on a 64-bit Windows, you will need to install a 32-bit version of MATLABTM to inter-operate with Webots. On Linux, the 32-bit version of Webots can only inter-operate with a 32-bit version of MATLABTM and the 64-bit version of Webots can only inter-operate with a 64-bit version of MATLABTM. 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 MATLABTM.
On some platform the MATLABTM interface needs perl and gcc to be installed separately. These tools are required because MATLABTM's loadlibrary() function will need to recompile Webots header files on the fly. According to MATLABTM'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 MATLABTM 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: