Skip to content

Commit

Permalink
fix Brainvision file issue when no event markers
Browse files Browse the repository at this point in the history
  • Loading branch information
arnodelorme committed Nov 11, 2021
1 parent a0190d8 commit bee8f87
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*~
30 changes: 16 additions & 14 deletions bids_export.m
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ function bids_export(files, varargin)
'anattype' '' {} 'T1w';
'chanlocs' '' {} '';
'chanlookup' 'string' {} '';
'interactive' 'string' {'on' 'off'} 'off';
'defaced' 'string' {'on' 'off'} 'on';
'createids' 'string' {'on' 'off'} 'on';
'singleEventsJson' 'string' {'on' 'off'} 'on';
Expand All @@ -277,21 +278,22 @@ function bids_export(files, varargin)

% deleting folder
fprintf('Exporting data to %s...\n', opt.targetdir);
if exist(opt.targetdir,'dir')
uilist = { ...
{ 'Style', 'text', 'string', 'Output directory exists and all current files will be deleted if continue', 'fontweight', 'bold' }, ...
{ 'Style', 'text', 'string', 'Would you want to proceed?'}, ...
};
geometry = { [1] [1]};
geomvert = [1 1 ];
[results,userdata,isOk,restag] = inputgui( 'geometry', geometry, 'geomvert', geomvert, 'uilist', uilist, 'title', 'Warning');
if isempty(isOk)
disp('BIDS export cancelled...')
return
else
disp('Deleting folder...')
rmdir(opt.targetdir, 's');
if exist(opt.targetdir,'dir')
if strcmpi(opt.interactive, 'on')
uilist = { ...
{ 'Style', 'text', 'string', 'Output directory exists and all current files will be deleted if continue', 'fontweight', 'bold' }, ...
{ 'Style', 'text', 'string', 'Would you want to proceed?'}, ...
};
geometry = { [1] [1]};
geomvert = [1 1 ];
[results,userdata,isOk,restag] = inputgui( 'geometry', geometry, 'geomvert', geomvert, 'uilist', uilist, 'title', 'Warning');
if isempty(isOk)
disp('BIDS export cancelled...')
return
end
end
disp('Folder exist. Deleting folder...')
rmdir(opt.targetdir, 's');
end

disp('Creating sub-directories...')
Expand Down
6 changes: 5 additions & 1 deletion pop_exportbids.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@
if ~isempty(pInfo)
options = [options 'pInfo' {pInfo}];
end
bids_export(subjects, options{:});
if nargin < 3
bids_export(subjects, 'interactive', 'on', options{:});
else
bids_export(subjects, options{:});
end
disp('Done');

% history
Expand Down
50 changes: 26 additions & 24 deletions rename_brainvision_files.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,33 @@ function rename_brainvision_files(varargin)
fclose(fid1);
fclose(fid2);

oldmarkerfile = fullfile(pathIn , oldmarkerfile);
newmarkerfile = fullfile(pathOut, newmarkerfile);
assert(exist(newmarkerfile, 'file')==0, 'the file %s already exists', newmarkerfile);
fid1 = fopen(oldmarkerfile, 'r');
fid2 = fopen(newmarkerfile, 'w');
if exist('oldmarkerfile', 'var')
oldmarkerfile = fullfile(pathIn , oldmarkerfile);
newmarkerfile = fullfile(pathOut, newmarkerfile);
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 = [baseNameOut switchcase(x)];
line = sprintf('HeaderFile=%s', newheaderfile);
elseif ~isempty(regexp(line, '^DataFile', 'once'))
[~, rem] = strtok(line, '=');
olddatafile = rem(2:end);
[~, ~, x] = fileparts(olddatafile);
newdatafile = [baseNameOut switchcase('.eeg')];
line = sprintf('DataFile=%s', newdatafile);
end
fprintf(fid2, '%s\r\n', line);
while ~feof(fid1)
line = fgetl(fid1);
if ~isempty(regexp(line, '^HeaderFile', 'once'))
[~, rem] = strtok(line, '=');
oldheaderfile = rem(2:end);
[~, ~, x] = fileparts(oldheaderfile);
newheaderfile = [baseNameOut switchcase(x)];
line = sprintf('HeaderFile=%s', newheaderfile);
elseif ~isempty(regexp(line, '^DataFile', 'once'))
[~, rem] = strtok(line, '=');
olddatafile = rem(2:end);
[~, ~, x] = fileparts(olddatafile);
newdatafile = [baseNameOut switchcase('.eeg')];
line = sprintf('DataFile=%s', newdatafile);
end
fprintf(fid2, '%s\r\n', line);
end
fclose(fid1);
fclose(fid2);
end
fclose(fid1);
fclose(fid2);

olddatafile = fullfile(pathIn , olddatafile);
newdatafile = fullfile(pathOut, newdatafile);
Expand All @@ -114,4 +116,4 @@ function rename_brainvision_files(varargin)
try delete(oldheaderfile); end
try delete(oldmarkerfile); end
try delete(olddatafile); end
end
end

0 comments on commit bee8f87

Please sign in to comment.