Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@robertoostenveld
Created September 25, 2018 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robertoostenveld/e31637a777c514bf1e86272e1092316e to your computer and use it in GitHub Desktop.
Save robertoostenveld/e31637a777c514bf1e86272e1092316e to your computer and use it in GitHub Desktop.
MATLAB function to rename all three files of a BrainVision EEG dataset. It also updates the details inside the files.
function rename_brainvision_files(oldheaderfile, newheaderfile)
% RENAME_BRAINVISION_FILES renames a BrainVision EEG dataset, which consists of a vhdr header
% file, vmrk marker file and a data file that usually has the extension dat, eeg or seg.
%
% Use as
% rename_brainvision_files(oldname, newname)
% where both the old and the new filename should be strings corresponding to the
% header file, i.e. including the vhdr extension.
%
% See also http://www.fieldtriptoolbox.org/ and https://sccn.ucsd.edu/wiki/EEGLAB for
% open source software to process BrainVision EEG data.
% determine whether the file extensions should be in lower or upper case
if ~isempty(regexp(newheaderfile, 'VHDR$', 'once'))
switchcase = @upper;
else
switchcase = @lower;
end
% determine the filename without extension
[~, f, ~] = fileparts(newheaderfile);
% deal with the header file
assert(exist(oldheaderfile, 'file')~=0, 'the file %s does not exists', oldheaderfile);
assert(exist(newheaderfile, 'file')==0, 'the file %s already exists', newheaderfile);
fid1 = fopen(oldheaderfile, 'r');
fid2 = fopen(newheaderfile, 'w');
while ~feof(fid1)
line = fgetl(fid1);
if ~isempty(regexp(line, '^MarkerFile', 'once'))
[~, rem] = strtok(line, '=');
oldmarkerfile = rem(2:end);
[~, ~, x] = fileparts(oldmarkerfile);
newmarkerfile = [f switchcase(x)];
line = sprintf('MarkerFile=%s', newmarkerfile);
elseif ~isempty(regexp(line, '^DataFile', 'once'))
[~, rem] = strtok(line, '=');
olddatafile = rem(2:end);
[~, ~, x] = fileparts(olddatafile);
newdatafile = [f switchcase(x)];
line = sprintf('DataFile=%s', newdatafile);
end
fprintf(fid2, '%s\r\n', line);
end
fclose(fid1);
fclose(fid2);
% deal with the marker file
assert(exist(oldmarkerfile, 'file')~=0, 'the file %s does not exists', oldmarkerfile);
assert(exist(newmarkerfile, 'file')==0, 'the file %s already exists', newmarkerfile);
fid1 = fopen(oldmarkerfile, 'r');
fid2 = fopen(newmarkerfile, 'w');
while ~feof(fid1)
line = fgetl(fid1);
if ~isempty(regexp(line, '^HeaderFile', 'once'))
[~, rem] = strtok(line, '=');
oldheaderfile = rem(2:end);
[~, ~, x] = fileparts(oldheaderfile);
newheaderfile = [f switchcase(x)];
line = sprintf('HeaderFile=%s', newheaderfile);
elseif ~isempty(regexp(line, '^DataFile', 'once'))
[~, rem] = strtok(line, '=');
olddatafile = rem(2:end);
[~, ~, x] = fileparts(olddatafile);
newdatafile = [f switchcase(x)];
line = sprintf('DataFile=%s', newdatafile);
end
fprintf(fid2, '%s\r\n', line);
end
fclose(fid1);
fclose(fid2);
% deal with the data file
assert(exist(olddatafile, 'file')~=0, 'the file %s does not exists', olddatafile);
assert(exist(newdatafile, 'file')==0, 'the file %s already exists', newdatafile);
status = copyfile(olddatafile, newdatafile);
if ~status
error('failed to copy data from %s to %s', olddatafile, newdatafile);
end
@CPernet
Copy link

CPernet commented Oct 2, 2018

here is a revision for some naming issues I had to fix and added option for removing old files
https://gist.github.com/CPernet/e037df46e064ca83a49fb4c595d4566a

no idea how to do a pull request with gist

@robertoostenveld
Copy link
Author

Also no clue how to merge this back. But your version and comment are nicely added to my gist, so I guess it is fine as it is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment