MATLAB Script to Import Custom Channel File

Hello, I am having a lot of trouble writing a script.

I have read the extensive documentation from the tutorial section, and have read into the internal variables and parameters in the functions/commands themselves. I am really at a loss here, and would greatly appreciate help.

I have a [time x channel] (32 channels) EEG .mat matrix and a custom channel file. I have this channel file as both a .mat struct, and an ASCII XYZ, Name format.

My goal is, without the GUI, to create a subject, import and assign the EEG matrix to the subject, and import and assign the channel file to the subject.

I successfully can create the subject, and can successfully import the EEG matrix. However, I am having trouble with the channel file. Once the EEG is imported, it appears the brainstorm GUI creates a default channel file "Brainstorm channels (32)". This channel appears to have the default channel names and channel locations.

When I try to import my own channel files, it does not change anything, nor does it import it.

This is what brainstorm displays:

To summarize, may someone tell me if there is a function/command to import a custom channel file, in either .txt "ASCII XYZ. Name" format OR a channel struct file (the layout of the struct is the correct format, I checked).

Thank you so much, and here is the code:

% Paths and filenames
addpath("D:\EEG_Epilepsy\Codes\Will Stuff\Functions");
addpath("D:\EEG_Epilepsy\Codes\Will Stuff\EEG Data");
eegPath = "D:\EEG_Epilepsy\Codes\Will Stuff\EEG Data";
subjectEEGpath = 'Data_EEGonly016.mat';
eegFullPath = fullfile(eegPath, subjectEEGpath);
channelpath = 'D:\EEG_Epilepsy\Codes\Will Stuff\EEG Data\Data_EEG_Channels.txt';

% subject info - using subject 16 as a place holder
% subjectEEGpath = 'Data_EEGonly016.mat';
subjectID = 'Subject016';

% create protocol - just run once then comment out
brainstorm nogui
% gui_brainstorm('CreateProtocol', 'DeltaTestingV2', 0, 0);

% Check if subject already exists
if isempty(bst_get('Subject', ID))
    db_add_subject(ID, [], 0, 0);
else
    fprintf('Skipped (already exists): %s\n', ID);
end

% Import EEG .mat file (make sure it's a matrix variable like "eeg" in .mat)
bst_process('CallProcess', 'process_import_data_raw', [], [], ...
    'subjectname',    subjectID, ...
    'condition',      '', ...
    'datafile',       {eegFullPath, 'EEG-MAT'}, ...   
    'DataMatTranspose', 1);

% import  channel file
bst_process('CallProcess', 'process_import_channel', [], [], ...
    'subjectname',  'Subject016', ...
    'channelfile',  {'D:\EEG_Epilepsy\Codes\Will Stuff\EEG Data\Data_EEG_Channels.txt', 'ASCII: XYZ'}, ...
    'usedefault',   0, ...
    'channelalign', 1);

I figured it out! For anyone in the future possibly having a similar issue:

My problem was that my script was doing the equivalent of right clicking on the created channel files, when what I needed to do was the equivalent of right clicking the folder labeled "raw".

Here's what I changed, this is to load a struct.

sFiles = bst_process('CallProcess', 'process_import_data_raw', [], [], ...
    'subjectname',    subjectID, ...
    'condition',      '', ...
    'datafile',       {eegFullPath, 'EEG-MAT'}, ...
    'UseDefaultChannel', 0, ...  % <- important: do NOT use default here
    'DataMatTranspose', 1);

% Step 2: Import channel file into this specific raw file
bst_process('CallProcess', 'process_import_channel', sFiles, [], ...
    'channelfile', {'D:\EEG_Epilepsy\Codes\Will Stuff\EEG Data\Subject016 Channel\newChannel2.mat', 'BST'}, ...
    'usedefault', 0, ...  % <- 0 = per-run (raw file), not subject-level
    'channelalign', 0);
1 Like

Hi @willbanks,

The process process_import_channel does the the same as right-click on the raw file and selecting Import channel file. There is not option Import channel file on right-clicking the channel file, the one there is Add EEG locations, and as its name indicates, it ONLY adds the locations, it does not change the number or names of the channels in the channel file, its equivalent process is process_channel_addloc

From the shared information and scripts it seems the issue at setting the correct format for the channel to your text file Data_EEG_Channels.txtis that it was not the correct format. In your description, it is indicated that the file is in the format ASCII XYZ, Name, but in the script the format (second element in the cell array) is 'ASCII: XYZ'.

This is not a valid format, it should be 'ASCII_XYZ'.
Or 'ASCII_XYZN' if there is a Name column

The second script does work because the valid format 'BST' was set.
Check the valid formats strings using for channel using:

bst_get('FileFilters', 'channel')

yes, you were correct! It was the 'ASCII_XYZN'. I figured it out by having Brainstorm generate a script that does what I wanted it too, then I looked inside the script it made to see what the correct parameter name was.

Thank you for your detailed response, it answered other questions about Brainstorm that I had (but did not post). This software is really incredible - you and your team did an amazing job.

Thanks again!

1 Like