From bee8f878dc5c0b09974801ae0dbd1a028a5123bc Mon Sep 17 00:00:00 2001 From: arnodelorme Date: Wed, 10 Nov 2021 20:10:38 -0800 Subject: [PATCH] fix Brainvision file issue when no event markers --- .gitignore | 1 + bids_export.m | 30 ++++++++++++----------- pop_exportbids.m | 6 ++++- rename_brainvision_files.m | 50 ++++++++++++++++++++------------------ 4 files changed, 48 insertions(+), 39 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/bids_export.m b/bids_export.m index 4368808..e11e3c5 100644 --- a/bids_export.m +++ b/bids_export.m @@ -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'; @@ -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...') diff --git a/pop_exportbids.m b/pop_exportbids.m index 9062a42..5aeb823 100644 --- a/pop_exportbids.m +++ b/pop_exportbids.m @@ -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 diff --git a/rename_brainvision_files.m b/rename_brainvision_files.m index b909184..8df8899 100644 --- a/rename_brainvision_files.m +++ b/rename_brainvision_files.m @@ -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); @@ -114,4 +116,4 @@ function rename_brainvision_files(varargin) try delete(oldheaderfile); end try delete(oldmarkerfile); end try delete(olddatafile); end -end \ No newline at end of file +end