Hello all,

I am testing BCI2000 for UDP connection for my future application. I ran BCI2000 with SignalGenerator, ARSignalProcessing, and CursorTask modules. Then, by using AppConnector help, and binding UDP ports in a MATLAB script by a code that I found online as follows:

--------------------------------------------------------------

u = udp( 'localhost', XXXXX, 'LocalPort' , YYYYY, 'Terminator' ,'CR/LF' , 'Timeout' , 10);

fopen (u);

v=fgetl(u);

while (v~=-1)

disp(v)

v=fgetl(u);

end

fclose (u);

delete (u);

-------------------------------------------------------------

I can see the continuous changes of states and data in command window in MATLAB. I am not professional in socket programming and I do not have any idea how I can manipulate and use these data. For the start, I have some questions. Any help will be appreciated.

1- I know that the line "while (v~=-1)" in code is for getting continuous data until timeout, but why this guy has used (-1)? Do I need to consider some changes in BCI2000 before running this code?

2- In the command window, I can see two repetitive lines as follows:

Signal (0,0) ####

Signal (1,0) ####

I think these are control signals from BCI2000 which are sent to CursorTask application module for x and y direction, respectively. Am I right? If yes, I am not sure why the indexes are in this way: (0,0) and (1,0)?

3- How can I filter these control signals for my application?

4- Instead of using script file in MATLAB, how we can use MATLAB simulink (UDP blocks) to bind the UPD ports and get these control signals in simulink environment in real-time.

5- It seems by starting BCI2000, these signals are sent through UDP port and to the MATLAB. But, we know that the real control time by the subject will happen after PreRunDuration+PreFeedbackDuration. I am not sure what is the best way to tell the external application (in MATLAB) to start when the FeedbackDuration time starts (in BCI2000).

Thanks,

Reza

## Reading BCI2000 data by MATLAB and throguh UDP port

### Re: Reading BCI2000 data by MATLAB and throguh UDP port

I assume that you already went through the BCI2000 App Connector description on the BCI2000 wiki [1]. I thus will focus on your specific questions.

Regards, Peter

[1] http://www.bci2000.org/wiki/index.php/T ... _Connector

[2] http://www.mathworks.com/help/matlab/ref/fgetl.html

[3] http://www.bci2000.org/wiki/index.php/U ... CursorTask

[4] http://www.mathworks.com/help/matlab/ref/regexp.html

[5] http://www.mathworks.com/help/simulink/ ... ction.html

[6] http://www.bci2000.org/wiki/index.php/U ... k#Timeline

At the end of the run BCI2000 will send an end of line tag that will cause the fgetl function to return -1 and thus the loop to terminate [2].1- I know that the line "while (v~=-1)" in code is for getting continuous data until timeout, but why this guy has used (-1)? Do I need to consider some changes in BCI2000 before running this code?

Correct, Signal(0,0) is X, Signal(1,0) is Y and Signal(2,0) is the Z dimension the CursorTask [3]. The reason that there are two indices are, that BCI200 allows to transfer multiple values per signal, however the CursorTask does not require this. Please be aware the BCI2000 uses C++ style 0-referenced indexing, unlike MATLAB which uses 1-referenced.2- In the command window, I can see two repetitive lines as follows:

Signal (0,0) ####

Signal (1,0) ####

I think these are control signals from BCI2000 which are sent to CursorTask application module for x and y direction, respectively. Am I right? If yes, I am not sure why the indexes are in this way: (0,0) and (1,0)?

I would suggest using a regular expression in MATLAB to filter the relevant control signals and states [4].3- How can I filter these control signals for my application?

You can include MATLAB functions in Simulink models (e.g., by using a MATLAB function block from the library [User-Defined Functions]). When you run the Simulink model the model-embedded MATLAB functions are automatically executed. You can have multiple MATLAB functions inside a Simulink model, simply drag the MATLAB function block from the Library as many times as you need, or, copy-paste the block inside the model. You can also call MATLAB functions (say, e.g., from one of these functions blocks) that are not part of the Simulink model as detailed in [5].4- Instead of using script file in MATLAB, how we can use MATLAB simulink (UDP blocks) to bind the UPD ports and get these control signals in simulink environment in real-time.

BCI2000 sends all states through the AppConnector. For your purpose you should use the Feedback state as detailed in the CursorTask time line [6].5- It seems by starting BCI2000, these signals are sent through UDP port and to the MATLAB. But, we know that the real control time by the subject will happen after PreRunDuration+PreFeedbackDuration. I am not sure what is the best way to tell the external application (in MATLAB) to start when the FeedbackDuration time starts (in BCI2000).

Regards, Peter

[1] http://www.bci2000.org/wiki/index.php/T ... _Connector

[2] http://www.mathworks.com/help/matlab/ref/fgetl.html

[3] http://www.bci2000.org/wiki/index.php/U ... CursorTask

[4] http://www.mathworks.com/help/matlab/ref/regexp.html

[5] http://www.mathworks.com/help/simulink/ ... ction.html

[6] http://www.bci2000.org/wiki/index.php/U ... k#Timeline

### Who is online

Users browsing this forum: Google [Bot] and 2 guests