%%%#!/usr/local/bin/matlab%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%O
% MATLAB Script to read BrainVision Data and export them in EEP Format
% 1. Read Brain Vision Data (.vhdr files)
% 2. Use the eepio plugin and export the data in the EEP Format
%
% Requires: eepio plugin (Download from https://github.com/widmann/eepio,
%           unpack the zipfile, copy the files into the plugins folder of EEGLab)
%
% Author: R. Muralikrishnan -- 07.03.2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%m
clear all;
close all;

%eeglab;

V_FolderIn = '/home/xyz/blablabla/FolderWhereBVFilesAre/';
V_DataFolder = '/home/xyz/blablabla/FolderWhereCNTFilesShouldBe/';


V_Participant = 'xx';

CA_Participants = {'Subj_01','Subj_02','Subj_03','Subj_28'};  % List of Participants
                  % If the .vhdr files are named as Subj_xy.vhdr, then Subj_xy is entered in this array
for V_Counter = 1:length(CA_Participants)

    V_Participant = CA_Participants{V_Counter};
    V_FileIn = fullfile(V_FolderIn, [V_Participant '.vhdr']);

    fprintf('########################################################\n\n');
    fprintf('Reading %s.\n\n', V_FileIn);

    % Read BrainVision .vhdr file

    EEG = pop_fileio(V_FileIn);

    CNT_filename = [V_Participant '.cnt'];
    TRG_filename = [V_Participant '.trg'];


    % Export CNT and TRG files using the eepio plugin

    pop_writeeepcnt(EEG, 'filename', CNT_filename, 'pathname', V_DataFolder);
    pop_writeeeptrg(EEG, 'filename', TRG_filename, 'pathname', V_DataFolder);



    fprintf('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n');
    fprintf('Exported Brain Vision Data as EEP CNT file ... %s.\n\n', V_Participant);
    fprintf('########################################################\n\n');

end;