[422372]: / functions / popfunc / pop_topochansel.m

Download this file

308 lines (277 with data), 8.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
% POP_TOPOCHANSEL - pop up a topographic interface to select channels
%
% Pops up a topographic interface to select multiple channels with the
% mouse. Click a polygon around the electrodes you wish to select. Right
% click to finish.
%
% Usage:
% >> [chanlist] = pop_topochansel(chanlocs,selection);
%
% Inputs:
% chanstruct - channel structure. See READLOCS
% (optional if EEG structure with chanlocs in caller or
% base workspace)
% selection - currently selected electrodes.
% (optional)
%
%
% Output:
% chanlist - indices of selected channels
% cellchannames - names of selected channel names in a cell array
% strchannames - names of selected channel names in a concatenated string
% (channel names are separated by space characters)
%
function [chanlist, cellchannames, strchannames] = pop_topochansel(chanlocs,select,varargin)
if nargin == 0 || isempty(chanlocs)
try
chanlocs = evalin('caller','chanlocs');
catch
try
tmp = evalin('caller','EEG');
chanlocs = tmp.chanlocs;
catch
error('tell me where the electrodes are'),
end
end
end
if nargin >= 2
if ischar(select)|| iscellstr(select)
select = chnb(select,{chanlocs.labels});
end
end
g = finputcheck( varargin, ...
{ 'labels' 'string' {'on' 'off'} 'off'
'cellstrout' 'string' {'on' 'off'} 'off'
},'ignore');
h0 = figure(23537);clf;
set(gcf,'numbertitle','off','name','Channel selection');
topoplot([],chanlocs,'electrodes','on', 'style','blank');
chi = get(gca,'children');
% chi = chi(1:numel(chanlocs));
todel = [];
for i = 1:numel(chi)
try
if numel(get(chi(i),'XData')) == sum(~emptycells({chanlocs.X}))
continue % we found electrodes
else
todel(end+1) = i;
end
catch
todel(end+1) = i;
end
end
chi(todel) = [];
hold on
X = get(chi,'XData');
Y = get(chi,'YData');
delete(chi)
if strcmp(g.labels,'on')
for i = 1:numel(X)
text(X(i),Y(i),chanlocs(i).labels);
end
end
if exist('select','var')
plot(X(select),Y(select),'r.','markersize',20);
else
select = [];
end
[dum dum chanlist] = lasso(X,Y);
if isempty(chanlist)
disp('No new elecs selected. Keeping old selection')
chanlist = select;
end
strchannames = '';
for i = 1:numel(chanlist)
strchannames = [strchannames chanlocs(chanlist(i)).labels];
if i ~= numel(chanlist)
strchannames = [strchannames ' '];
end
cellchannames{i} = chanlocs(chanlist(i)).labels;
end
if strcmp(g.cellstrout,'on')
chanlist = cellchannames;
end
if nargout == 0
disp(strchannames)
disp(chanlist)
clear
end
% close(h0);
function [selx,sely,indexnr]=lasso(x,y)
% lasso - enables the selection/encircling of (clusters of) events in a scatter plot by hand
% using the mouse
%
% Input: x,y - a set of points in 2 column vectors.
% Output: selx,sely,indexnr - a set of selected points in 3 column vectors
%
% Note: After the scatter plot is given, selection by mouse is started after any key press.
% This is done to be able to ZOOM or CHANGE AXES etc. in the representation before selection
% by mouse.
% Encircling is done by pressing subsequently the LEFT button mouse at the requested positions
% in a scatter plot.
% Closing the loop is done by a RIGHT button press.
%
% T.Rutten V2.0/9/2003
% downloaded and adapted from mathworks fileexchange by M. Chaumon 2011
plot(x,y,'ob')
las_x=[];
las_y=[];
c=1;
key=0;
while c==1
try
[a,b,c]=ginput(1);
las_x=[las_x;a];las_y=[las_y;b];
line(las_x,las_y)
catch
selx = [];sely = []; indexnr = [];
return
end
end
las_x(length(las_x)+1)=las_x(1);
las_y(length(las_y)+1)=las_y(1);
line(las_x,las_y)
pause(.2)
in=inpolygon(x,y,las_x,las_y);
ev_in=find(in>0);
selx=x(ev_in);
sely=y(ev_in);
plot(selx,sely,'r.','markersize',20);
drawnow
indexnr=ev_in;
function nb = chnb(channame, varargin)
% CHNB - return channel number corresponding to channel names in an EEG
% structure
%
% Usage:
% >> [nb] = chnb(channame);
% >> [nb] = chnb(channame, labels);
%
% Input:
% channame - name of channels to search. Either a string with space
% separated channel names, or a cell array of strings.
% Note that regular expressions can be used to match
% several channels. See regexp.
% labels - channel labels as found in EEG.chanlocs.labels.
%
% Output:
% nb - channel numbers in the EEG structure found in the
% caller workspace (i.e. where the function is called
% from) or in the base workspace, if no EEG structure
% exists in the caller workspace.
%
error(nargchk(1,2,nargin));
if nargin == 2
labels = varargin{1};
else
try
EEG = evalin('caller','EEG');
catch
try
EEG = evalin('base','EEG');
catch
error('Could not find EEG structure');
end
end
if not(isfield(EEG,'chanlocs'))
error('No channel list found');
end
labels = {EEG.chanlocs.labels};
end
if ischar(channame)
tmp = regexp(channame,'(\S*) ?','tokens');
channame = {};
for i = 1:numel(tmp)
channame{i} = tmp{i}{1};
end
if isempty(channame)
nb = [];
return
end
end
nb = regexpcell(labels,channame,'exactignorecase');
function idx = regexpcell(c,pat, cmds)
% idx = regexpcell(c,pat, cmds)
%
% Return indices idx of cells in c that match pattern(s) pat (regular expression).
% Pattern pat can be char or cellstr. In the later case regexpcell returns
% indexes of cells that match any pattern in pat.
%
% cmds is a string that can contain one or several of these commands:
% 'inv' return indexes that do not match the pattern.
% 'ignorecase' will use regexpi instead of regexp
% 'exact' performs an exact match (regular expression should match the whole strings in c).
% 'all' (default) returns all indices, including repeats (if several pat match a single cell in c).
% 'unique' will return unique sorted indices.
% 'intersect' will return only indices in c that match ALL the patterns in pat.
%
% v1 Maximilien Chaumon 01/05/09
% v1.1 Maximilien Chaumon 24/05/09 - added ignorecase
% v2 Maximilien Chaumon 02/03/2010 changed input method.
% inv,ignorecase,exact,combine are replaced by cmds
error(nargchk(2,3,nargin))
if not(iscellstr(c))
error('input c must be a cell array of strings');
end
if nargin == 2
cmds = '';
end
if not(isempty(regexpi(cmds,'inv', 'once' )))
inv = true;
else
inv = false;
end
if not(isempty(regexpi(cmds,'ignorecase', 'once' )))
ignorecase = true;
else
ignorecase = false;
end
if not(isempty(regexpi(cmds,'exact', 'once' )))
exact = true;
else
exact = false;
end
if not(isempty(regexpi(cmds,'unique', 'once' )))
combine = 2;
elseif not(isempty(regexpi(cmds,'intersect', 'once' )))
combine = 3;
else
combine = 1;
end
if ischar(pat)
pat = cellstr(pat);
end
if exact
for i_pat = 1:numel(pat)
pat{i_pat} = ['^' pat{i_pat} '$'];
end
end
for i_pat = 1:length(pat)
if ignorecase
trouv = regexpi(c,pat{i_pat}); % apply regexp on each pattern
else
trouv = regexp(c,pat{i_pat}); % apply regexp on each pattern
end
idx{i_pat} = [];
for i = 1:numel(trouv)
if not(isempty(trouv{i}))% if there is a match, store index
idx{i_pat}(end+1) = i;
end
end
end
switch combine
case 1
idx = [idx{:}];
case 2
idx = unique([idx{:}]);
case 3
for i_pat = 2:length(pat)
idx{1} = intersect(idx{1},idx{i_pat});
end
idx = idx{1};
end
if inv % if we want to invert result, then do so.
others = 1:numel(trouv);
others(idx) = [];
idx = others;
end