---
title: OSC in Matlab on Windows, Linux and Mac OS X using Java
canonical: https://0110.be/posts/OSC_in_Matlab_on_Windows%2C_Linux_and_Mac_OS_X_using_Java
markdown_url: https://0110.be/posts/OSC_in_Matlab_on_Windows%2C_Linux_and_Mac_OS_X_using_Java.md
id: 430
published_at: '2015-03-25T00:00:00Z'
updated_at: '2025-11-29T21:40:18Z'
author: Joren
tags:
- name: Code
  markdown_url: https://0110.be/tags/Code.md
- name: UGent
  markdown_url: https://0110.be/tags/UGent.md
---

# OSC in Matlab on Windows, Linux and Mac OS X using Java

<img src="https://0110.be/files/attachments/430/matlab.gif" width="150px" alt="matlab logo" style="float:right">This post explains how to receive [OSC](http://opensoundcontrol.org/-messages) 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](http://opensoundcontrol.org/implementation/matlab-osc). This method uses a library called [liblo](http://liblo.sourceforge.net/) 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](http://www.aboehler.at/doku/doku.php/blog:2009:0727_install_osc_for_matlab_on_windows). Obviously not for the faint of heart.

An alternative way leverages the [Matlab facilities to run Java](http://nl.mathworks.com/help/matlab/matlab_external/bringing-java-classes-and-methods-into-matlab-workspace.html#f46352). Since there is a [Java OSC](http://www.illposed.com/software/javaoscdoc/) library available ([JavaOSC on github](https://github.com/hoijui/JavaOSC)) it is relatively easy to bridge the two. To make the connection, I have written some [glue code](https://0110.be/files/attachments/430/MatlabOSCListener.java) and provide an easy to use [Jar-library](https://0110.be/files/attachments/430/javaosctomatlab.jar) here. Using the bridge is done as follows:

### How to make Matlab receive OSC-messages

1.  Download the "JavaOSCtoMatlab Java library":\[javaosctomatlab.jar\] and store it in an easy to remember directory.
2.  Download the "example Matlab OSC client Script":\[osc_java_test.m\] and store it in the same directory. The client is included below as well.
3.  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 `cd` command points to the directory with the downloaded jar-file.
4.  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 `Object[]`, `double[]` or `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 `osc_listener.getMessageArguments()`, `osc_listener.getMessageArgumentsAsDouble()`, `osc_listener.getMessageArgumentsAsString()`.

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;
````


- [javaosctomatlab.jar](https://0110.be/files/attachments/430/javaosctomatlab.jar)

- [osc\_java\_test.m](https://0110.be/files/attachments/430/osc_java_test.m)

- [JavaOSCToMatlab.zip](https://0110.be/files/attachments/430/JavaOSCToMatlab.zip)

- [MatlabOSCListener.java](https://0110.be/files/attachments/430/MatlabOSCListener.java)

- [matlab.gif](https://0110.be/files/attachments/430/matlab.gif)
