Jump to content

Contributions:BCI2000MatlabInterface

From BCI2000 Wiki

Synopsis

This is an example that controlling BCI2000 under Windows from MATLAB. Matlab is able to interface with dynamic libraries(.dll,.so,.dylib) if a header file is available.(http://www.mathworks.com/help/matlab/using-c-shared-library-functions-in-matlab-.html). Thus we can load the BCI2000RemoteLib library by specifying prog/BCI2000RemoteLib.dll as C shared library and src/core/Operator/BCI2000Remote/BCI2000RemoteLib.h as header file when calling MATLAB's loadlibrary function. When using BCI2000RemoteLib this way, your MATLAB code will run on all platforms supported by BCI2000, and no COM registration will be required on Windows systems.

Author

Huiling Huang <huiling@neurotechcenter.org>

Functional Description

BCI2000Remote is an interface to the BCI2000 Operator module, that allows to start-up, configure, and run BCI2000 from other applications. For more BCI2000 Operator module commands, please refer to User_Reference:Operator_Module_Scripting

Examples

%% c library load, initial part
clc;clear;
if not(libisloaded('bci'))
    loadlibrary('C:\BCI2000.x64\prog\BCI2000RemoteLib','C:\BCI2000.x64\src\core\Operator\BCI2000Remote\BCI2000RemoteLib.h', 'alias', 'bci')
end
libfunctions('bci')
%need to call BCI2000Remote_Delete to recovery the memory
bciHander = calllib('bci', 'BCI2000Remote_New');
calllib('bci', 'BCI2000Remote_SetOperatorPath', bciHander,'C:/BCI2000.x64/prog/Operator');
if calllib('bci', 'BCI2000Remote_Connect', bciHander) ~= 1
    fprintf('bci connect fail!')
    calllib('bci', 'BCI2000Remote_Delete', bciHander);
    return
end
calllib('bci', 'BCI2000Remote_Execute', bciHander,'Change directory $BCI2000LAUNCHDIR',0);

calllib('bci', 'BCI2000Remote_SetWindowVisible', bciHander,1);
modules = libpointer('stringPtrPtr', {'SignalGenerator', 'SpectralSignalProcessing', 'CursorTask'});
calllib('bci', 'BCI2000Remote_StartupModules2', bciHander, modules, 3);
calllib('bci', 'BCI2000Remote_LoadParametersRemote', bciHander, '../parms/examples/CursorTask_SignalGenerator.prm');
%%
%add states
calllib('bci', 'BCI2000Remote_AddStateVariable', bciHander,'matlab',8, 0);

calllib('bci', 'BCI2000Remote_SetConfig', bciHander);

calllib('bci', 'BCI2000Remote_Execute', bciHander,'Show window watches',0);
calllib('bci', 'BCI2000Remote_Execute', bciHander,'visualize watch matlab',0);
%start
calllib('bci', 'BCI2000Remote_Start', bciHander);
pause(5);

%% send the behavior data to BCI2000
for i = 1:10
    calllib('bci', 'BCI2000Remote_SetStateVariable', bciHander,'matlab', i);
    pause(1);
end