EEGLAB Import .vhdr (bva) files and save as .set in a loop
Show older comments
Hello,
first of all, I'm a beginner in coding so please forgive my bad style. Some of the code may be just copied from the GUI.
I am trying to load EEG files in.vhdr (Brain Vision) format and save them as individual .set files using EEGLAB.
I'm also trying to use a loop.
This is my code now:
%%
allmyfiles = dir('*.vhdr');
allmyfilenames = string({allmyfiles.name})';
newsetnames = [ "name1.set" "name2.set" ];
for n = 1:size(allmyfilenames,1)
bvsetName = allmyfiles(n).name;
newsetname = newsetnames(n);
EEG = pop_loadbv('c:mypath1', bvsetName, [], [] );
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 0,'gui','off');
EEG = eeg_checkset( EEG );
EEG = pop_saveset( EEG, 'filename',newsetname,'filepath','c:mypath2');
[ALLEEG EEG] = eeg_store(ALLEEG, EEG, CURRENTSET);
EEG = pop_delset( EEG, [1] );
STUDY = []; CURRENTSTUDY = 0; ALLEEG = []; EEG=[]; CURRENTSET=[];
end
%%
The newsetname variable is a string variable, but I keep getting this error and I can't solve it.
Error using pop_saveset (line 117)
error: argument 'filename' must be a string
Thank you very much to those who will try to help me.
3 Comments
Alexander Craik
on 23 Jun 2022
put 'newsetnames(n)' in char()
i.e. newsetname = char(newsetnames(n));
I ran into the same problem and I was putting my filename in string() to check. char() fixed it for me.
FRANCESCA MITI
on 24 Jun 2022
Chris Winnard
on 4 Dec 2023
@Alexander Craik this was a big help to me too, thanks. :)
Answers (1)
Öznur
on 13 Mar 2025
0 votes
Hi,
I would like to upload EEG files in .vhdr (Brain Vision) format and save them as separate .set files using EEGLAB. I wanted to use your code above but I got the following error. Can you help me, please?
%%
allmyfiles = dir('C:\Users\IDU\Downloads\mbagul1.vhdr');
allmyfilenames = string({allmyfiles.name})';
newsetnames = [ "name1.set" "name2.set" ];
for n = 1:size(allmyfilenames,1)
bvsetName = allmyfiles(n).name;
newsetname = char(newsetnames(n));
EEG = pop_loadbv('c:mypath1', bvsetName, [], [] );
[ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 0,'gui','off');
EEG = eeg_checkset( EEG );
EEG = pop_saveset( EEG, 'filename',newsetname,'filepath','c:mypath2');
[ALLEEG EEG] = eeg_store(ALLEEG, EEG, CURRENTSET);
EEG = pop_delset( EEG, [1] );
STUDY = []; CURRENTSTUDY = 0; ALLEEG = []; EEG=[]; CURRENTSET=[];
end
%%
And it returns:
Unrecognized function or variable 'pop_loadbv'.
Error in eed (line 8)
EEG = pop_loadbv('c:mypath1', bvsetName, [], [] );
Thank you, best regards..
1 Comment
JinYang
on 1 Apr 2025
%%
allmyfiles = dir('yourbrainvisionrawfilepath\*.vhdr'); %change the path to your brainvision file path
allmyfilenames = string({allmyfiles.name})';
for n = 1:size(allmyfilenames,1)
bvsetName = allmyfiles(n).name;
newsetname = char(allmyfilenames(n,1));
EEG = pop_loadbv('yourbrainvisionrawfilepath', bvsetName, [], []); %change the path to your brainvision file path
[ALLEEG, EEG, CURRENTSET] = pop_newset(ALLEEG, EEG, 0,'setname',newsetname,'gui','off'); %setname is set to be same as the vhdr raw file, delete('setname',newsetname,) if you don't need a setname
EEG = eeg_checkset( EEG );
EEG = pop_saveset( EEG,'filename',newsetname,'filepath','savepath'); %change the path to where you want to save the .set file
[ALLEEG, EEG] = eeg_store(ALLEEG, EEG, CURRENTSET);
clear EEG;
STUDY = []; CURRENTSTUDY = 0; ALLEEG = []; EEG=[]; CURRENTSET=[];
end
%%
Hi, not sure if you still need this, made some changes and it worked fine on my PC, also you may need to add eeglab to your matlab setpath and start eeglab, then just close it before running this program.
Categories
Find more on EEG/MEG/ECoG in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!