Calling yarp from Matlab
Instructions for calling YARP from Matlab are now officilly maintained on the YARP online documentation:
For instructions on the installation on a Mac OSX, you can refer to Calling yarp from Matlab on Mac OSX
These instructions are considered obsolete and will be removed.
Install SWIG and the Java Development Kit. Ubuntu Linux:
apt-get install swig default-jdk
Read Murielle Richard's tutorial: File:Yarp4Matlab.pdf, File:Yarp4Matlab.doc. It was written for Windows, but it is useful for other operating systems, too. Errata: the command in the second window terminal should be
yarp connect /matlab/write /matlab/read
In the remainder of this article, paths such as
example/swig are meant to be relative to the YARP source code directory:
- cmake example/swig
- in CMake, set the following variables and the following paths (adjust them to your particular installation):
- generate and compile the example SWIG project
You should obtain:
- a number of *.java files in
- a shared library called jyarp.dll (Windows) or libjyarp.so (Linux) in
Next, compile all the Java classes in
javac -source 1.3 -target 1.3 *.java
Next, copy the *.class files in two directories as follows. LoadYarp.class and YarpImageHelper.class go to (say)
~/jyarp; all the remaining files go to
~/jyarp/yarp. (This is because the latter files have to be in a directory called "yarp", like the corresponding Java package.) Add
~/jyarp to MATLAB's classpath.txt file (read below for further information about this).
Now, either copy the shared library to a location that is in your PATH, or put its path in MATLAB's librarypath.txt.
Now, either the
example/swig/generated_src directory to your CLASSPATH environment variable, or define MATLAB's classpath.txt as explained below.
Optional step: to generate the Java class documentation, go to
example/swig/generated_src and type:
javadoc -d ..\doc\html *.java
Common problems and workarounds
- If the yarp2/example/swig compilation fails due to a CMake error, check that
JAVA_AWT_LIBRARYis set to
- Sometimes, inside
CMakeLists.txtyou have to change the line
in order to get rid of an error about output directories. If you need to do this, then ignore the "
generated_src" part from all the tips.
Define the location of class files
You need to tell MATLAB the location of the YARP *.class files. Do this by adding the following line to your classpath.txt:
(Replace YOUR_YARP_PATH with the full path to YARP)
You can ask MATLAB about the location of your classpath.txt by typing
Usually it is
You need to tell Matlab the location of the yarp shared library (e.g. jyarp.dll in windows and libjyarp.so in linux) file. Do this by adding the following line to your librarypath.txt:
(Replace YOUR_YARP_PATH here with the full path to YARP)
You can ask Matlab about the location of your librarypath.txt by typing
Usually it is
Call YARP within MATLAB
Now the next time you run matlab you should be able to call yarp code.
For example try:
LoadYarp; % this loads the yarp library and initializes the network b = yarp.Bottle; b.fromString('hello from MATLAB')
Smarter examples can be found in
Common problems and workarounds
- You need to close and run MATLAB again every time you change your classpath.txt file or the .java/.class.
- If MATLAB for Linux gives errors like "libstdc++.so.6 version `CXXABI_1.3.1' not found", then you are experiencing a conflict among libraries. In particular, the library libstdc++.so.6 used by MATLAB may differ from the library used by your C++ compiler. Sometimes the problem can be fixed by replacing the MATLAB libraries with the C++ library used by the compiler. Be careful, though, as this might stop MATLAB from working. Another solution is to compile ACE, YARP and the code in
$YARP_ROOT/example/swigwith the same version of the compiler that was used to compile your copy of MATLAB.
- If MATLAB for Linux gives errors like "relocation R_X86_64_32 against `__gxx_personality_v0' can not be used when making a shared object; recompile with -fPIC /home/icub/Code/yarp2/lib/libYARP_dev.a: could not read symbols: Bad value collect2: ld returned 1 exit status", try to recompile yarp2 with the variable
- (Advice from Breno Carneiro Pinheiro) It may prove necessary to replace both ligcc_s.so.1 and libstdc++.so in MATLABPATH/glnx86/ to the one in system /usr/lib/
- (Advice from Breno Carneiro Pinheiro) It may be necessary to change declarations like port=Port in /swig/matlab/yarp_read and yarp_write to port=yarp.Port
- (Advice from Breno Carneiro Pinheiro) There's more help at http://wiki.epfl.ch/lasa/yarp-matlab