Matlab script for opening .edf files using brainstorm

I’m new to MATLAB and Brainstorm. I’ve managed to convert several files to .edf format using Python, but when I attempt to load them in Neuroscore, I encounter an error indicating that the files are either corrupt or in the wrong format. Strangely enough, these files load without issue in Brainstorm.

I’m currently working on a script in Brainstorm that would automatically load all .edf files from a specified folder, reconvert them to .edf using Brainstorm’s export to file feature, and then save them in a different folder.

Below is my attempt at the script:

data_folder = 'C:\Users\garim';
edf_files = dir(fullfile(data_folder, '*.edf'));
for i = 1:length(edf_files)
    edf_file = fullfile(data_folder, edf_files(i).name);
    fprintf('Processing file: %s\n', edf_file);
    [data, annotations] = edfread(edf_file);
end

can someone help with the issue ?

You may want to check first the .edf from Python with a tool such as EDFbrowser to see what is the issue with the files.
EDFbrowser

You can also try to read and write using Matlab native functions: edfread() and starting from R2021a edfwrite()

Brainstorm do not use Matlab's edfread() to read EDF data. So, if you want to conversion with Brainstorm, first try with the GUI

  1. Add the .edf file for review, and verity its contents
    Tutorials 1 to 5 in the Brainstorm tutorials: https://neuroimage.usc.edu/brainstorm/Tutorials

  2. Right-click in the Link to raw file > File > Export to file

If it this works fine in the GUI, then you can make a Brainstorm pipeline with two process:

  1. Import > Import recordings > Create link to raw file
  2. File > Export to file

Such a pipeline can be converted to a Matlab scrtipt:
https://neuroimage.usc.edu/brainstorm/Tutorials/Scripting

I’ve been using Brainstorm to convert EEG data files using the GUI successfully. However, I now have a large number of files, and I want to automate the process for all of them. However, when i run the script in the matlab it is not getting executed. The input file in GUI is .edf and the output is also convert to .edf. The confusion is coming from the sFiles.

I generated the following MATLAB script based on a tutorial:

% Input files
sFiles = {...
    'NewSubject/Default/data_block001_02.mat'};
SubjectNames = {...
    'NewSubject'};
RawFiles = {...
    'C:\Users\garim\OneDrive\Desktop\block001_02.edf'};

% Start a new report
bst_report('Start', sFiles);

% Process: Export to file: Data
sFiles = bst_process('CallProcess', 'process_export_file', sFiles, [], ...
    'exportdata', {RawFiles{1}, 'EEG-EDF'});

% Process: Import MEG/EEG: Time
sFiles = bst_process('CallProcess', 'process_import_data_time', sFiles, [], ...
    'subjectname',   SubjectNames{1}, ...
    'condition',     '', ...
    'timewindow',    [0, 3719.9995], ...
    'split',         0, ...
    'ignoreshort',   1, ...
    'usectfcomp',    1, ...
    'usessp',        1, ...
    'freq',          [], ...
    'baseline',      [], ...
    'blsensortypes', 'MEG, EEG');

% Save and display report
ReportFile = bst_report('Save', sFiles);
bst_report('Open', ReportFile);
% bst_report('Export', ReportFile, ExportDir);
% bst_report('Email', ReportFile, username, to, subject, isFullReport);

% Delete temporary files
% gui_brainstorm('EmptyTempFolder');

However, I want to run this script automatically for multiple files. How can I achieve this?

This are good news.

For the task you aim sFiles should be empty, there is not data imported.
The problem is in your script it does what you attempt, the shared scripts does:

  1. Export a data file that is already imported as EDF+ (this is not what you want, you want to create the link to raw first)
  2. Imports data from the created EDF+ (again, this is not what you want, you are not importing that file)

You need to do a pipeline that does: (1) create of link to raw file (original EDF file), and (2) export it as the new EDF file. The steps are indicated above:

It would look like:
|| image | image ||

When it does what you want, then generate the pipeline script and add the FOR loop for all your files

I was able to generate this script, based on the above:

% Script generated by Brainstorm (28-Mar-2024)

% Input files
sFiles = [];
SubjectNames = {...
    'NewSubject'};
RawFiles = {...
    'C:\Users\garim\OneDrive\Desktop\l8_trial_brainstorm.edf', ...
    'C:\Users\garim\OneDrive\Desktop\l8_trial_brainstorm_11.edf'};

% Start a new report
bst_report('Start', sFiles);

% Process: Create link to raw file
sFiles = bst_process('CallProcess', 'process_import_data_raw', sFiles, [], ...
    'subjectname',    SubjectNames{1}, ...
    'datafile',       {RawFiles{1}, 'EEG-EDF'}, ...
    'channelreplace', 1, ...
    'channelalign',   1, ...
    'evtmode',        'value');

% Process: Export to file: Raw
sFiles = bst_process('CallProcess', 'process_export_file', sFiles, [], ...
    'exportraw', {RawFiles{2}, 'EEG-EDF'});

% Save and display report
ReportFile = bst_report('Save', sFiles);
bst_report('Open', ReportFile);
% bst_report('Export', ReportFile, ExportDir);
% bst_report('Email', ReportFile, username, to, subject, isFullReport);

% Delete temporary files
% gui_brainstorm('EmptyTempFolder');

can you help me loop it ?

Hi @Kshitij, regarding your request, the Brainstorm-generated script from above already does the EDF conversion for a pair of files (old and new). So, you will need to write a FOR loop in Matlab. If you are not familiar with Matlab, please check the link below where we have compile resources. The purpose of this forum is to help with the use of the Brainstorm software. Unfortunately, we do not have the extra resources to write general scripts for you.

P.S. We do reply the forum questions as soon as possible. Please do not spam the post with "could you please reply" messages, they do not make us to reply sooner.