~ OSC in Matlab on Windows, Linux and Mac OS X using Java» By Joren on Wednesday 25 March 2015
This post explains how to receive OSC in a MatLab environment. It uses a platform independent Java library which should work on 64 and 32 bit versions of Windows, Unix and Mac OS X. Using Java makes installation relatively easy compared with other solutions.
The most used method to get OSC-messages in Matlab can be found here. This method uses a library called liblo which needs to be configured (compiled) correctly on your system. Especially on Windows this can be problematic. A brave soul documented his quest to get OSC working with Matlab on Windows here. Obviously not for the faint of heart.
An alternative way leverages the Matlab facilities to run Java. Since there is a Java OSC library available (JavaOSC on github) it is relatively easy to bridge the two. To make the connection, I have written some glue code and provide an easy to use Jar-library here. Using the bridge is done as follows:
How to make Matlab receive OSC-messages
- Download the JavaOSCtoMatlab Java library and store it in an easy to remember directory.
- Download the example Matlab OSC client Script and store it in the same directory. The client is included below as well.
- Start Matlab, modify the client script to fit your needs. You probably need to change the OSC method to listen to and the OSC port. Also make sure that the
cdcommand points to the directory with the downloaded jar-file.
- Run the client script and receive your OSC messages.
Note that there are three ways to receive the payload of a message. They are returned by the Java code as either
String. The last two are automatically understood by Matlab, so they are more easy to work with. Respectively to get the message data you need to call either
I hope this is useful to some…
cd('C:/dir/with/jar/file/') % Check your java version 1.6+ should be ok version -java % Load the jar file javaaddpath('javaosctomatlab.jar'); % Import the needed java packages import com.illposed.osc.*; import java.lang.String % defines the OSC port to listen to receiver = OSCPortIn(4000); % defines the OSC method to listen to osc_method = String('/ECG'); osc_listener = MatlabOSCListener(); receiver.addListener(osc_method,osc_listener); receiver.startListening(); %infinite loop, receiving all non empty messages while(1) struct = osc_listener.getMessageArgumentsAsDouble(); if ~isempty(struct) struct end end receiver.stopListening(); receiver=0;