Introduction

As i’ve been starting to work with EEG/BCI and especially P300, I’ve gone back to using trusty old MATLAB for programming. Previously I’ve only been using matlab for calculation and simple statistics but now I’m starting to use it for Simulink. So this will be an intro to Simulink and a guide in how to use Simulink with Unity.

Implementation

First let’s create a new Simulink window in Matlab and add a few objects. Under Tools you will find the Simulink Library Browser and in there, search and drag out a UDP Receive block, a Data Type Conversion block and a To File block. I’ve had both success and failure using the Display block, so make sure we are getting values from Unity, i’ll save them in a file.

´´

Connect the UDP Receive to the Data Type Conversion to the To File, as the image above shows. The Simulink file can also be found on github.

When the Simulink file is done it is time to look at the Unity project. What we are going to do is a basic C# implementation of a UDP connection. I expect that readers know the basic on Unity, so I’m just going to talk about the code.

Start of my making a new C# script and attach it to an object (I’m just using the Main Camera). Open the script and start out my including:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

Before we go to the Start() and Update() functions, we need a few variables:

private UdpClient udpClient;
private string address;
private IPEndPoint ipEndPoint;
private byte[] sendBytes;
private byte[] zero = Encoding.ASCII.GetBytes("0");

In the Start() function we need to setup a new UdpClient and connect to out IP address and Port. I’m going to send the ASCII version of the number 1 to Simulink. This can easily be done using code following code:

udpClient = new UdpClient();
address = "127.0.0.1";
ipEndPoint = new IPEndPoint(IPAddress.Parse(address), 2814);
sendBytes = Encoding.ASCII.GetBytes("1");
udpClient.Connect(ipEndPoint);

Since we are just working locally, the IP address will just be localhost (127.0.0.1). The actual code to send the information to Simulink is placed in the Update() function.

if(Input.GetKeyDown("space")) {
	try {
		udpClient.Send(sendBytes, sendBytes.Length);
	} catch (Exception e) {
		Debug.Log("Something went wrong: " + e);
	}
}

The program should now work and the complete unity code can be found on github. You can now start the Simulink patch, then the Unity program and press space a few files. Then stop both programs and you should now have a file called unity2simulink.mat in your default matlab folder (normally places in Documents/MATLAB/).

Let’s look at the saved data. To do this we need to open matlab and load in the file.

load unity2simulink.mat

You will now have a variable called ans in your workspace. Whenever Simulink saves a file, the first row is the timestamp. In this case we don’t care about that, so let’s remove it and set all it’s values to 0.

ans(1,:)=0;

Now we want the unique values from the matrix, to get them we write:

values = unique(ans);

values is now a vector with the two unique values we have. These will be 0 and 49. The last thing we want to do is to count the unique values. This we can do using histc:

instances = histc(ans(:),values);

instances should now contain two values. A huge number which is the amount of zeroes we have, we don’t care about that, and then a smaller number which correspond to the 1’s we have. The complete matlab code is:

load unity2simulink.mat
ans(1,:)=0;
values = unique(ans);
instances = histc(ans(:),values);
instances(2)