Skip to content

Instantly share code, notes, and snippets.

@chassall
Created May 5, 2017 23:19
Show Gist options
  • Save chassall/44fd0b084fa1e9664e649e36a1f0966a to your computer and use it in GitHub Desktop.
Save chassall/44fd0b084fa1e9664e649e36a1f0966a to your computer and use it in GitHub Desktop.
Modify a BrainVision EEG marker file.
% Open the old marker file
filename = 'oldFile.vmrk';
fileId = fopen(filename);
% Open a new marker file for writing
newFilename = 'newFile.vmrk';
newFileId = fopen(newFilename,'w');
% Get a line from the old file
thisLine = fgets(fileId);
% Loop through each line of the old file
while ischar(thisLine)
% If this line includes a marker, e.g. 'S 1' through 'S255'
if ~isempty(regexp(thisLine, 'S [1-9]|S [1-9][0-9]|S[1-2][0-9][0-9]', 'once'))
[matchStart, matchEnd] = regexp(thisLine, 'S [1-9]|S [1-9][0-9]|S[1-2][0-9][0-9]'); % Get start/end characters
thisMarkerString = thisLine(matchStart + 1:matchEnd); % Get this marker's string
thisMarker = str2double(thisMarkerString); % Convert to a number for modification
newMarker = thisMarker; % Modify marker as needed
newMarkerString = sprintf('S%3d',newMarker); % Make a string for new marker
thisLine(matchStart:matchEnd) = newMarkerString; % Modify line from original marker file
end
% Write to new file
fwrite(newFileId,thisLine);
% Get next line
thisLine = fgets(fileId);
end
% Close old and new marker files
fclose(fileId);
fclose(newFileId);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment