Calling yarp from Matlab on Mac OSX

From Wiki for iCub and Friends
Jump to navigation Jump to search

This page is a step-to-step how to install yarp for matlab on a Mac OSX. It roughly follows instructions of Calling yarp from Matlab and File:Yarp4Matlab.pdf.


Install Yarp

Please follow instructions from Homebrew or MacOSX: installation to install Yarp.

Install Swig

You can install swig from its source code SWIG (Simplified Wrapper and Interface Generator) or with homebrew:

 brew install swig

Yarp side

In the following we note $YARP_DIR the directory where your yarp code is located. If you installed it with homebrew, YARP_DIR=/Library/Caches/Homebrew/yarp--svn-HEAD/ or YARP_DIR=/Library/Caches/Homebrew/yarp-VersionNo/

 cd $YARP_ROOT/bindings
 mkdir build
 cd build
 ccmake ../

In cmake, hit 'c' to configure and toggle CREATE_JAVA on. Hit 'c' to configure again, 'g' to generate. Compile with 'make' in order to generate files. You should obtain:

  • a number of *.java files in $YARP_ROOT/bindings/build/generated_src/java
  • a shared library called libjyarp.jnilib in $YARP_ROOT/bindings/build/
 cd src
 cp *.java ../generated_src/
 cd ../generated_src/∞∞

Compile all the Java classes in example/swig/generated_src:

 javac -source 1.3 -target 1.3 *.java

Copy the classes to different folders. Let's say you want to store it in folder $JYARP_DIR

 cp LoadYarp.class $JYARP_DIR
 cp YarpImageHelper.class $JYARP_DIR
 mkdir $JYARP_DIR/yarp
 cp *.class $JYARP_DIR/yarp/
 cd $JYARP_DIR/yarp/
 rm LoadYarp.class
 rm YarpImageHelper.class

Matlab side

Edit Matlab's classpath.txt and librarypath.txt (which you can find the path with the matlab command

 which classpath.txt

It usually is in folder /Applications/MATLAB_*.app/toolbox/local/

Add to both files the paths:


Test call yarp within matlab

First test

In matlab command window, run:

 LoadYarp; % this loads the yarp library and initializes the   network
 b.fromString('hello from MATLAB')

This should not output any error (otherwise refer to the paragraph "Possible issues". And the last command should output

 hello from MATLAB

Second test

First find yarp_read.m and yarp_write.m in folder $YARP_DIR/example/matlab. In the current version of yarp, you might want to edit them as in File:Yarp read.m and File:Yarp write.m , ie: you need to :

  • add "import yarp.Port"
  • add "import yarp.Bottle"
  • change "disp(b);" to "disp(b.toString_c());"

Then, open a terminal and launch

 yarp server

Open a first matlab command window (A) and go to $YARP_DIR/example/matlab and launch


Open a second matlab command window (B) and go to $YARP_DIR/example/matlab and launch


Open a terminal and launch

 yarp connect /matlab/write /matlab/read

Now you can go to matlab window A and type any text. It should echo in matlab window B. To quit, type 'quit' in matlab window A

Possible issues

Some issues have been reported:


Errors might happen when you call from Matlab


If a message error

 LoadYarp; ??? "Undefined function or variable 'LoadYarp'"

appears, it means that you did not edit the classpath.txt and librarypath.txt files to point to the location of LoadYarp.class

If a message error

 ?? Java exception occurred:
 java.lang.NoClassDefFoundError: yarp/Network
 at LoadYarp.<init>( 

appears, it means that it did not find file Network.class. Maybe it is because of the classpath.txt and librarypath.txt again. Network.class needs to be in a directory named "yarp", which in turn needs to be in a directory which also contains LoadYarp.class and YarpImageHelper.class