1
+ % eeg_importcoordsystemfiles - import coordinate information
2
+ %
3
+ % Usage:
4
+ % [EEG, bids] = eeg_importcoordsystemfiles(EEG, coordfile, 'key', value)
5
+ %
6
+ % Inputs:
7
+ % 'EEG' - [struct] the EEG structure to which event information will be imported
8
+ % coordfile - [string] path to the coordsystem.json file.
9
+ % e.g. ~/BIDS_EXPORT/sub-01/ses-01/eeg/sub-01_ses-01_task-GoNogo_coordsystem.json
10
+ %
11
+ % Optional inputs:
12
+ % 'bids' - [struct] structure that saves imported BIDS information. Default is []
13
+ %
14
+ % Outputs:
15
+ % EEG - [struct] the EEG structure with event info imported
16
+ % bids - [struct] structure that saves BIDS information with event information
17
+ %
18
+ % Authors: Arnaud Delorme, 2022
19
+
20
+ function [EEG , bids ] = eeg_importcoordsystemfiles(EEG , coordfile , varargin )
21
+
22
+ if nargin < 2
23
+ help eeg_importcoordsystemfiles ;
24
+ return ;
25
+ end
26
+
27
+ g = finputcheck(varargin , {' bids' ' struct' [] struct([]) }, ' eeg_importcoordsystemfiles' , ' ignore' );
28
+ if isstr(g ), error(g ); end
29
+
30
+ bids = g .bids ;
31
+
32
+ % coordinate information
33
+ bids(1 ).coordsystem = loadfile( coordfile , ' ' );
34
+
35
+ if isfield(bids .coordsystem , ' AnatomicalLandmarkCoordinates' ) && ~isempty(bids .coordsystem .AnatomicalLandmarkCoordinates )
36
+ fieldNames = fieldnames(bids .coordsystem .AnatomicalLandmarkCoordinates );
37
+ if ~isfield(EEG .chaninfo , ' nodatchans' )
38
+ EEG.chaninfo.nodatchans = [];
39
+ end
40
+ for iField = 1 : length(fieldNames )
41
+ EEG .chaninfo .nodatchans(end + 1 ).labels = fieldNames{iField };
42
+ EEG .chaninfo .nodatchans(end ).X = bids .coordsystem .AnatomicalLandmarkCoordinates.(fieldNames{iField })(1 );
43
+ EEG .chaninfo .nodatchans(end ).Y = bids .coordsystem .AnatomicalLandmarkCoordinates.(fieldNames{iField })(2 );
44
+ EEG .chaninfo .nodatchans(end ).Z = bids .coordsystem .AnatomicalLandmarkCoordinates.(fieldNames{iField })(3 );
45
+ end
46
+ EEG.chaninfo.nodatchans = convertlocs(EEG .chaninfo .nodatchans );
47
+ end
48
+
49
+ % import JSON or TSV file
50
+ % -----------------------
51
+ function data = loadfile(localFile , globalFile )
52
+ [~ ,~ ,ext ] = fileparts(localFile );
53
+ data = [];
54
+ localFile = dir(localFile );
55
+ if ~isempty(localFile )
56
+ if strcmpi(ext , ' .tsv' )
57
+ data = importtsv( fullfile(localFile(1 ).folder, localFile(1 ).name));
58
+ else
59
+ if exist(' jsondecode.m' ,' file' )
60
+ data = jsondecode( importalltxt( fullfile(localFile(1 ).folder, localFile(1 ).name) ));
61
+ else
62
+ data = jsonread(fullfile(localFile(1 ).folder, localFile(1 ).name));
63
+ end
64
+ end
65
+ elseif ~isempty(globalFile )
66
+ if strcmpi(ext , ' .tsv' )
67
+ data = importtsv( fullfile(globalFile(1 ).folder, globalFile(1 ).name));
68
+ else
69
+ if exist(' jsondecode.m' ,' file' )
70
+ data = jsondecode( importalltxt( fullfile(globalFile(1 ).folder, globalFile(1 ).name) ));
71
+ else
72
+ data = jsonread(fullfile(globalFile(1 ).folder, globalFile(1 ).name));
73
+ end
74
+ end
75
+ end
76
+
77
+ % Import full text file
78
+ % ---------------------
79
+ function str = importalltxt(fileName )
80
+
81
+ str = [];
82
+ fid = fopen(fileName , ' r' );
83
+ while ~feof(fid )
84
+ str = [str 10 fgetl(fid ) ];
85
+ end
86
+ str(1 ) = [];
87
+
88
+ function nameout = cleanvarname(namein )
89
+
90
+ % Routine to remove unallowed characters from strings
91
+ % nameout can be use as a variable or field in a structure
92
+
93
+ % custom change
94
+ if strcmp(namein ,' #' )
95
+ namein = ' nb' ;
96
+ end
97
+
98
+ % 1st char must be a letter
99
+ for l= 1 : length(namein )
100
+ if isletter(namein(l ))
101
+ nameout = namein(l : end );
102
+ break % exist for loop
103
+ end
104
+ end
105
+
106
+ % remove usual suspects
107
+ stringcheck = [strfind(namein ,' (' ), ...
108
+ strfind(namein ,' )' ) ...
109
+ strfind(namein ,' [' ) ...
110
+ strfind(namein ,' ]' ) ...
111
+ strfind(namein ,' {' ) ...
112
+ strfind(namein ,' }' ) ...
113
+ strfind(namein ,' -' ) ...
114
+ strfind(namein ,' +' ) ...
115
+ strfind(namein ,' *' ) ...
116
+ strfind(namein ,' /' ) ...
117
+ strfind(namein ,' #' ) ...
118
+ strfind(namein ,' %' ) ...
119
+ strfind(namein ,' &' ) ...
120
+ strfind(namein ,' @' ) ...
121
+ ];
122
+
123
+ if ~isempty(stringcheck )
124
+ nameout(stringcheck ) = [];
125
+ end
126
+
127
+ % last check
128
+ if ~isvarname(nameout )
129
+ error(' the variable name to use is still invalid, check chars to remove' )
130
+ end
0 commit comments