Skip to content

Tutorial 4 a. Receive Data streams in MATLAB

Chadwick Boulay edited this page Dec 23, 2019 · 2 revisions

LSL also provides the possibility to easily read in data streams with the programming language of your choice (almost). Here you can find example code on how to receive (and send) data for different programming languages (C, C++, Python, Java, C#, Matlab).

In this tutorial, you will learn how to receive data streams and marker streams in MATLAB.

For this tutorial you need

  • A Windows Computer with a microphone (For this tutorial you should reduce the sampling frequency of your audio interface. )
  • MATLAB
  • The LSL AudioCapture App (for streaming the microphone input)
  • The LSL Keyboard App (for streaming key presses and releases)
  • The LSL Mouse App (for streaming mouse button presses and releases and mouse positions) You could use the LabRecorder for saving all data streams for an offline analysis of the data.

The LSL App can be downloaded from the ftp as .zip files.


  • Start LabRecorder.exe
  • Start AudioCaptureWin.exe; Press ‘Link’
  • Start Keyboard.exe; Press ‘Link’

For receiving a data stream in MATLAB you:

  • Instantiate the library
  • Look for the stream (in this example of type Audio)
  • Create the inlet
  • Get data from the inlet
%% instantiate the library
disp('Loading the library...');
lib = lsl_loadlib();

% resolve a stream...
disp('Resolving an EEG stream...');
result = {};
while isempty(result)
    result = lsl_resolve_byprop(lib,'type','Audio'); end

% create a new inlet
disp('Opening an inlet...');
inlet = lsl_inlet(result{1});

disp('Now receiving data...');
while true
    % get data from the inlet
    [vec,ts] = inlet.pull_sample();
    % and display it
    fprintf('%.2f\t',vec);
    fprintf('%.5f\n',ts);
end

Execute this code to see the incoming data on the command line. You can stop the executing of the code with'Control-C'


For receiving a marker stream in MATLAB you:

  • Instantiate the library
  • Look for the stream (in this example of type Marker)
  • Create the inlet
  • Get data from the inlet
% instantiate the library
disp('Loading the library...');
lib = lsl_loadlib();

% resolve a stream...
disp('Resolving a Markers stream...');
result = {};
while isempty(result)
    result = lsl_resolve_byprop(lib,'type','Markers'); end

% create a new inlet
disp('Opening an inlet...');
inlet = lsl_inlet(result{1});

disp('Now receiving data...');
while true
    % get data from the inlet
    [mrks,ts] = inlet.pull_sample();
    % and display it
    fprintf('got %s at time %.5f\n',mrks{1},ts);
end

Execute this code to see the incoming data on the command line. Every time you press or release a key on your keyboard you should see a new line in the command line displaying the name of the key, whether it is pressed or released and the time of the keypress. You can stop the executing of the code with 'Control-C'.*


Instead of using the LSL Keyboard App, you can try the same with the LSL Mouse App (for streaming mouse button presses and releases and mouse positions).

  • Start Mouse.exe; Press ‘Link’

Resolving a Marker stream by 'type' (as in the examples above) is possible. However, when multiple Marker streams are available, this approach fails. The

while isempty(result)
    result = lsl_resolve_byprop(lib,'type','Markers');

The statement will look for the first stream of the type Markers. But, this is not necessarily the stream you are looking for. Instead, you can resolve a stream by 'name'. For the MousePosition stream this would be:

while isempty(result)
    result = lsl_resolve_byprop(lib,'name','MousePosition'); 

or alternatively for the mouse buttons

while isempty(result)
    result = lsl_resolve_byprop(lib,'name','MouseButtons'); 

Mark that in the example code below the while loop is also changed, to present the mouse positions on the command line correctly.

% instantiate the library
disp('Loading the library...');
lib = lsl_loadlib();

% resolve a stream...
disp('Resolving a Markers stream...');
result = {};
while isempty(result)
    result = lsl_resolve_byprop(lib,'name','MousePosition'); end

% create a new inlet
disp('Opening an inlet...');
inlet = lsl_inlet(result{1});

disp('Now receiving data...');
while true
    % get data from the inlet
    [pos,ts] = inlet.pull_sample();
    % and display it
    fprintf('%.2f\t',pos);
    fprintf('%.5f\n',ts);
end

On the command line, you should now see the x and y coordinates of your mouse and the time stamps of the data. Note, that this is not a continuous data stream, data is only generated while you are moving the mouse.


In Tutorial 4b we combine the above examples to preform online data processing depending on some incoming marker event.