-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathpop_selectevent.m
598 lines (558 loc) · 25.2 KB
/
pop_selectevent.m
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
% POP_SELECTEVENT - Find events in an EEG dataset. If the dataset
% is the only input, a window pops up to
% ask for the relevant parameter values.
%
% Usage: >> [EEGOUT,event_indices] = pop_selectevent( EEG, 'key1', value1, ...
% 'key2', value2, ... );
% Input:
% EEG - EEG dataset
%
% Optional inputs:
% 'latency' - [latency_range] latency range of events to include
% Ex: 'latency','400 <= 700' Include all events with
% latnecy in the range [400,700]
% 'omitlatency' - [latency_range] latency range of events to exclude
% 'type' - [type_range] event type(s) to include
% Ex: 'type',3 or [2 3 5] or 1:10
% 'omittype' - [type_range], type(s) of events to exclude
% 'event' - [event_range], indices of events to include
% 'omitevent' - [event_range], indices of events to exclude
% 'USER_VAR' - [VAR_range], 'USER_VAR' is any user-defined field in
% the event structure. Includes events with values of
% field 'USER_VAR' in the specified range. Use [vector]
% format for integers, 'min<max' format for real numbers.
% 'omitUSER_VAR' - [VAR_range], 'USER_VAR' range of events to exclude
% 'select' - ['normal'|'inverse'] invert the selection of events.
% {Default is 'normal'}
% 'deleteepochs' - ['on'|'off'] 'on' = Delete ALL epochs that do not include
% any of the specified events {Default = 'on'}.
% This option is relevant only for epoched datasets derived
% from continuous datasets.
% 'invertepochs' - ['on'|'off'] 'on' = Invert epoch selection. {Default = 'off'}.
% 'deleteevents' - ['on'|'off'] 'on' = Delete ALL events except
% the selected events. {Default = 'off'}.
% 'renametype' - [string] rename the type of selected events with the
% string given as parameter. {Default is [], do not rename
% field}.
% 'oldtypefield' - [string] in conjunction with the previous parameter,
% create a new field (whose 'name' is provided as parameter)
% to store the (old) type of the event whose type has been
% renamed. {Default is [], do not create field}.
%
% Outputs:
% EEGOUT - EEG dataset with the selected events only
% event_indices - indexes of the selected events
%
% Ex: [EEGTARGETS,target_indices] = pop_selectevent(EEG,'type',[1 6 11 16 21]);
%
% % Returns ONLY THOSE epochs containing any of the 5 specified
% types of target events.
%
% Note: By default, if several optional inputs are given, the function
% performs their conjunction (&).
% Boundary events are keeped by default??. (6/12/2014 Ramon)
%
% Author: Arnaud Delorme, CNL / Salk Institute, 27 Jan 2002-
%
% See also: EEG_EVENTFORMAT, POP_IMPORTEVENT
% Copyright (C) Arnaud Delorme, CNL / Salk Institute, 27 Jan 2002, arno@salk.edu
%
% This file is part of EEGLAB, see http://www.eeglab.org
% for the documentation and details.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
% THE POSSIBILITY OF SUCH DAMAGE.
%02/01/2002 added inputgui and finalize function - ad
%02/06/2002 work on the header and event format - sm & ad
%02/09/2002 modify function according to new structure - ad
%02/12/2002 add getepoch compatibility - ad
%02/19/2002 add event indices & correct event selection - ad
function [EEG, Ievent, com] = pop_selectevent(EEG, varargin)
if nargin < 1
help pop_selectevent;
return;
end
Ievent = [];
com ='';
% note that this function is also used for epochs
% -----------------------------------------------
I = [];
if isempty(EEG(1).event)
disp('pop_selectevent: cannot deal with empty event structure');
return;
end
if length(EEG) > 1
disp('pop_selectevent: dealing with a group of dataset, assuming they all have a similar structure');
end
% remove the event field if present
% ---------------------------------
allfields = fieldnames(EEG(1).event);
indexmatch = strmatch('urevent', allfields);
if ~isempty(indexmatch)
allfields = { allfields{1:indexmatch-1} allfields{indexmatch+1:end} };
end
if nargin<2
geometry = { [0.6 2.1 1.2 0.8 ] };
uilist = { ...
{ 'Style', 'text', 'string', 'Field', 'horizontalalignment', 'center', 'fontweight', 'bold' }, ...
{} ...
{ 'Style', 'text', 'string', 'Selection', 'fontweight', 'bold' }, ...
{ 'Style', 'text', 'string', 'Set=NOT THESE', 'fontweight', 'bold' } };
% add all fields to graphic interface
% -----------------------------------
ind1 = strmatch('type', allfields, 'exact');
ind2 = strmatch('latency' , allfields, 'exact');
ind3 = strmatch('duration', allfields, 'exact');
neworder = [ ind2 ind3 ind1 setdiff(1:length(allfields), [ind1 ind2 ind3]) ];
allfields = { allfields{neworder} };
for index = 1:length(allfields)
% format the description to fit a help box
% ----------------------------------------
if index <= length( EEG(1).eventdescription )
tmptext = EEG(1).eventdescription{ neworder(index) };
if ~isempty(tmptext)
if size(tmptext,1) > 15, stringtext = [ tmptext(1,1:15) '...' ];
else stringtext = tmptext(1,:);
end
else stringtext = 'no description'; tmptext = 'no description (use menu Edit > Event Field)';
end
else stringtext = 'no description'; tmptext = 'no description (use menu Edit > Event Field)';
end
descrip = { 'string', stringtext, 'callback', ['questdlg2(' vararg2str(tmptext) ...
',''Description of field ''''' allfields{index} ''''''', ''OK'', ''OK'');' ] };
% create the gui for this field
% -----------------------------
textfield = allfields{index};
if strcmp(textfield, 'latency') || strcmp(textfield, 'duration')
if EEG(1).trials > 1, textfield = [ textfield ' (ms)' ];
else textfield = [ textfield ' (s)' ];
end
middletxt = { { 'Style', 'text', 'string', 'min' } { 'Style', 'edit', 'string', '' 'tag' [ 'min' allfields{index} ] } ...
{ 'Style', 'text', 'string', 'max' } { 'Style', 'edit', 'string', '' 'tag' [ 'max' allfields{index} ] } };
middlegeom = [ 0.3 0.35 0.3 0.35 ];
elseif strcmp(textfield, 'type')
commandtype = [ 'tmpEEG = get(gcbf, ''userdata'');' ...
'if ~isfield(tmpEEG.event, ''type'')' ...
' errordlg2(''No type field'');' ...
'else' ...
' tmpevent = tmpEEG.event;' ...
' if isnumeric(tmpEEG.event(1).type),' ...
' [tmps,tmpstr] = pop_chansel(unique([ tmpevent.type ]));' ...
' else,' ...
' [tmps,tmpstr] = pop_chansel(unique({ tmpevent.type }));' ...
' end;' ...
' if ~isempty(tmps)' ...
' set(findobj(''parent'', gcbf, ''tag'', ''type''), ''string'', tmpstr);' ...
' end;' ...
'end;' ...
'clear tmps tmpv tmpEEG tmpevent tmpstr tmpfieldnames;' ];
middletxt = { { 'Style', 'edit', 'string', '' 'tag' 'type' } { 'Style', 'pushbutton', 'string', '...' 'callback' commandtype } };
middlegeom = [ 0.95 0.35 ];
else
middletxt = { { 'Style', 'edit', 'string', '' 'tag' textfield } };
middlegeom = 1.3;
end
geometry = { geometry{:} [0.55 0.65 middlegeom 0.1 0.22 0.1] };
uilist = { uilist{:}, ...
{ 'Style', 'text', 'string', textfield }, ...
{ 'Style', 'pushbutton', descrip{:}, 'horizontalalignment', 'left' }, ...
middletxt{:}, ...
{ }, { 'Style', 'checkbox', 'string', ' ' 'tag' [ 'not' allfields{index} ] },{ } };
end
% event indices
% -------------
uilist = { uilist{:} ...
{ 'Style', 'text', 'string', 'Event indices' }, ...
{ }, ...
{ 'Style', 'edit', 'string', '' 'tag' 'indices' }, ...
{ }, { 'Style', 'checkbox', 'string', ' ' 'tag' 'notindices' },{ } };
geometry = { geometry{:} [0.55 0.65 1.3 0.1 0.22 0.1] };
% rename/keep events
% ------------------
geometry = { geometry{:} 1 1 [.1 2 .3 .2] [.1 1.5 0.5 0.5] [.1 1 0.5 1] [.1 1 0.5 1] };
uilist = { uilist{:} { } ...
{ 'Style', 'text', 'string','Event selection', 'fontweight', 'bold' } ...
{} { 'Style', 'checkbox', 'string','Select all events NOT selected above (Set this button and "all BUT" buttons (above) for logical OR)' 'tag' 'invertevent' } { } { } ...
{} { 'Style', 'checkbox', 'string','Keep only selected events and remove all other events', ...
'value', fastif(EEG(1).trials>1, 0, 1) 'tag' 'rmevents' } { } { } ...
{} { 'Style', 'text', 'string', 'Rename selected event type(s) as type:' } ...
{ 'Style', 'edit', 'string', '' 'tag' 'rename' } { } ...
{} { 'Style', 'text', 'string', 'Retain old event type name(s) in (new) field named:' } ...
{ 'Style', 'edit', 'string', '' 'tag' 'retainfield' } { } };
% epoch selections
% ----------------
if EEG(1).trials > 1
geometry = { geometry{:} 1 [0.1 2 0.5 0.5] [0.1 2 0.5 0.5]};
uilist = { uilist{:} ...
{ 'Style', 'text', 'string','Epoch selection', 'fontweight', 'bold' } ...
{ } { 'Style', 'checkbox', 'string','Remove epochs not referenced by any selected event', ...
'value', 1 'tag' 'rmepochs' } { } { } ...
{ } { 'Style', 'checkbox', 'string','Invert epoch selection', ...
'value', 0 'tag' 'invertepoch' } { } { } };
end
[results, ~, ~, res] = inputgui( 'geometry', geometry, 'uilist', uilist, 'helpcom', 'pophelp(''pop_selectevent'')', 'title', 'Select events -- pop_selectevent()', 'userdata', EEG(1));
if isempty(results), return; end
% decode inputs
% -------------
args = {};
if ~res.notindices, args = { args{:}, 'event', eval( [ '[' res.indices ']' ]) };
else args = { args{:}, 'omitevent', eval( [ '[' res.indices ']' ]) };
end
for index = 1:length(allfields)
textfield = allfields{index};
tmpflag = getfield(res, [ 'not' textfield ]);
if strcmp(textfield, 'duration') || strcmp(textfield, 'latency')
tmpres = [];
minlat = getfield(res, [ 'min' textfield ]);
maxlat = getfield(res, [ 'max' textfield ]);
if ~isempty(minlat) && ~isempty(maxlat)
tmpres = [ minlat '<=' maxlat ];
end
else
tmpres = getfield(res, textfield);
try
tmpres2 = eval( [ '[' tmpres ']' ] );
if ~isnumeric(tmpres2)
if tmpres(1) == ''''
tmpres = eval( [ '{' tmpres '}' ] );
else
tmpres = parsetxt( tmpres );
end
else
tmpres = tmpres2;
end
catch, tmpres = parsetxt( tmpres ); end
end
if ~isempty(tmpres)
if ~tmpflag, args = { args{:}, textfield, tmpres };
else args = { args{:}, [ 'omit' textfield], tmpres };
end
end
end
if res.invertevent, args = { args{:}, 'select', 'inverse' }; end
if ~isempty(res.rename), args = { args{:}, 'renametype', res.rename }; end
if ~isempty(res.retainfield), args = { args{:}, 'oldtypefield', res.retainfield }; end
args = { args{:}, 'deleteevents', fastif(res.rmevents, 'on', 'off') };
if EEG(1).trials > 1
args = { args{:}, 'deleteepochs', fastif(res.rmepochs , 'on', 'off') };
args = { args{:}, 'invertepochs', fastif(res.invertepoch , 'on', 'off') };
end
else % no interactive inputs
args = varargin;
end
% process multiple datasets
% -------------------------
if length(EEG) > 1
if nargin < 2
[ EEG, com ] = eeg_eval( 'pop_selectevent', EEG, 'warning', 'on', 'params', args);
else
[ EEG, com ] = eeg_eval( 'pop_selectevent', EEG, 'warning', 'off', 'params',args);
end
return;
end
% setting default for the structure
% ---------------------------------
fieldlist = { 'event' 'integer' [] [1:length(EEG.event)] ;
'omitevent' 'integer' [] [] ;
'deleteepochs' 'string' { 'yes','no','on','off' } 'on' ;
'invertepochs' 'string' { 'on','off' } 'off' ;
'deleteevents' 'string' { 'yes','no','on','off' } 'off';
'renametype' 'string' [] '';
'oldtypefield' 'string' [] '';
'select' 'string' { 'normal','inverse','remove','keep' } 'normal' };
for index = 1:length(allfields)
fieldlist{end+1, 1} = allfields{index};
fieldlist{end , 2} = '';
fieldlist{end+1, 1} = [ 'omit' allfields{index} ];
fieldlist{end , 2} = '';
end
g = finputcheck( args, fieldlist, 'pop_selectevent');
if ischar(g), error(g); end
if isempty(g.event), g.event = [1:length(EEG.event)]; end
if strcmpi(g.select, 'remove'), g.select = 'inverse'; end
if strcmpi(g.select, 'keep' ), g.select = 'normal'; end
if strcmpi(g.deleteepochs, 'yes' ), g.deleteepochs = 'on'; end
if strcmpi(g.deleteepochs, 'no' ), g.deleteepochs = 'off'; end
if ~isempty(g.oldtypefield) && isempty(g.renametype)
error('A name for the new type must be defined');
end
% select the events to keep
% -------------------------
Ievent = g.event;
Ieventrem = g.omitevent;
for index = 1:length(allfields)
% convert the value if the field is a string field
% ------------------------------------------------
tmpvar = getfield(g, {1}, allfields{index});
if ~isempty(tmpvar)
if isnumeric(tmpvar)
if ischar(getfield( EEG.event, {1}, allfields{index}))
for tmpind = 1:length(tmpvar)
tmpvartmp{tmpind} = num2str(tmpvar(tmpind));
end
tmpvar = tmpvartmp;
end
elseif ischar(tmpvar) && isempty( findstr(tmpvar, '<='))
if isnumeric(getfield( EEG.event, {1}, allfields{index}))
error(['numerical values must be entered for field ''' allfields{index} '''']);
end
end
end
if ischar(tmpvar) && isempty( findstr(tmpvar, '<='))
tmpvar = { tmpvar };
end
% scan each field of EEG.event
% ----------------------------
if ~isempty( tmpvar )
if iscell( tmpvar ) % strings
eval( [ 'tmpevent = EEG.event; tmpvarvalue = {tmpevent(:).' allfields{index} '};'] );
Ieventtmp = [];
for index2 = 1:length( tmpvar )
tmpindex = transpose(find(strcmp(deblank(tmpvar{index2}), deblank(tmpvarvalue))));%Ramon: for bug 1318. Also for compatibility(strmatch will be deleted in next versions of MATLAB)
%tmpindex = strmatch( tmpvar{index2}, tmpvarvalue, 'exact');
if isempty( tmpindex ),
fprintf('Warning: ''%s'' field value ''%s'' not found\n', allfields{index}, tmpvar{index2});
end
Ieventtmp = unique_bc( [ Ieventtmp; tmpindex ]);
end
Ievent = intersect_bc( Ievent, Ieventtmp );
elseif ischar( tmpvar ) % real range
tmpevent = EEG.event;
% ======== JRI BUGFIX 3/6/14
%eval( [ 'tmpvarvalue = [ tmpevent(:).' allfields{index} ' ];'] );
tmpvarvalue = safeConcatenate(EEG.event, allfields{index});
min = eval(tmpvar(1:findstr(tmpvar, '<=')-1));
max = eval(tmpvar(findstr(tmpvar, '<=')+2:end));
if strcmp(allfields{index}, 'latency')
if EEG.trials > 1
tmpevent = EEG.event;
tmpvarvalue = eeg_point2lat(tmpvarvalue, {tmpevent.epoch}, EEG.srate, ...
[EEG.xmin EEG.xmax]*1000, 1E-3);
else
tmpvarvalue = eeg_point2lat(tmpvarvalue, ones(1,length(EEG.event)), EEG.srate, ...
[EEG.xmin EEG.xmax], 1);
end
end
if strcmp(allfields{index}, 'duration')
if EEG.trials > 1, tmpvarvalue = tmpvarvalue/EEG.srate*1000;
else tmpvarvalue = tmpvarvalue/EEG.srate;
end
end
Ieventlow = find( tmpvarvalue >= min);
Ieventhigh = find( tmpvarvalue <= max);
Ievent = intersect_bc( Ievent, intersect( Ieventlow, Ieventhigh ) );
else
if strcmp(allfields{index}, 'latency')
fprintf(['pop_selectevent warning: latencies are continuous values\n' ...
'so you may use the ''a<=b'' notation to select these values\n']);
end
% ======== JRI BUGFIX 3/6/14
%eval( [ 'tmpevent = EEG.event; tmpvarvalue = [ tmpevent(:).' allfields{index} ' ];'] );
tmpvarvalue = safeConcatenate(EEG.event, allfields{index});
Ieventtmp = [];
for index2 = 1:length( tmpvar )
Ieventtmp = unique_bc( [ Ieventtmp find(tmpvarvalue == tmpvar(index2)) ] );
end
Ievent = intersect_bc( Ievent, Ieventtmp );
end
end
% scan each field of EEG.event (omit)
% -----------------------------------
tmpvar = eval(['g.omit' allfields{index} ]);
if eval(['ischar(EEG.event(1).' allfields{index} ')' ]) && isnumeric(tmpvar) && ~isempty(tmpvar)
for tmpind = 1:length(tmpvar)
tmpvartmp{tmpind} = num2str(tmpvar(tmpind));
end
tmpvar = tmpvartmp;
end
if ischar(tmpvar) && isempty( findstr(tmpvar, '<='))
tmpvar = { tmpvar };
end
if ~isempty( tmpvar )
if iscell( tmpvar )
eval( [ 'tmpevent = EEG.event; tmpvarvalue = {tmpevent(:).' allfields{index} '};'] );
Ieventtmp = [];
for index2 = 1:length( tmpvar )
tmpindex = strmatch( tmpvar{index2}, tmpvarvalue, 'exact');
if isempty( tmpindex ),
fprintf('Warning: ''%s'' field value ''%s'' not found\n', allfields{index}, tmpvar{index2});
end
Ieventtmp = unique_bc( [ Ieventtmp; tmpindex ]);
end
Ieventrem = union_bc( Ieventrem, Ieventtmp );
elseif ischar( tmpvar )
tmpevent = EEG.event;
% ======== JRI BUGFIX 3/6/14
%eval( [ 'tmpvarvalue = [ tmpevent(:).' allfields{index} ' ];'] );
tmpvarvalue = safeConcatenate(EEG.event, allfields{index});
min = eval(tmpvar(1:findstr(tmpvar, '<=')-1));
max = eval(tmpvar(findstr(tmpvar, '<=')+2:end));
if strcmp(allfields{index}, 'latency')
if EEG.trials > 1
tmpevent = EEG.event;
tmpvarvalue = eeg_point2lat(tmpvarvalue, {tmpevent.epoch}, EEG.srate, ...
[EEG.xmin EEG.xmax]*1000, 1E-3);
else
tmpvarvalue = eeg_point2lat(tmpvarvalue, ones(1,length(EEG.event)), EEG.srate, ...
[EEG.xmin EEG.xmax], 1);
end
end
if strcmp(allfields{index}, 'duration')
if EEG.trials > 1, tmpvarvalue = tmpvarvalue/EEG.srate*1000;
else tmpvarvalue = tmpvarvalue/EEG.srate;
end
end
Ieventlow = find( tmpvarvalue > min);
Ieventhigh = find( tmpvarvalue < max);
Ieventrem = union_bc( Ieventrem, intersect( Ieventlow, Ieventhigh ) );
else
if strcmp(allfields{index}, 'latency')
fprintf(['pop_selectevent warning: latencies are continuous values\n' ...
'so you may use the ''a<=b'' notation to select these values\n']);
end
tmpevent = EEG.event;
% ======== JRI BUGFIX 3/6/14
%eval( [ 'tmpvarvalue = [ tmpevent(:).' allfields{index} ' ];'] );
tmpvarvalue = safeConcatenate(EEG.event, allfields{index});
Ieventtmp = [];
for index2 = 1:length( tmpvar )
Ieventtmp = unique_bc( [ Ieventtmp find( tmpvarvalue ==tmpvar(index2)) ] );
end
Ieventrem = union_bc( Ieventrem, Ieventtmp );
end
end
end
Ievent = setdiff_bc( Ievent, Ieventrem);
if strcmp(g.select, 'inverse')
Ievent = setdiff_bc( [1:length(EEG.event)], Ievent );
end
% checking if trying to remove boundary events (in continuous data)
eeglab_options;
if ~isempty(EEG.event) && isfield(EEG.event, 'type')
if ischar(EEG.event(1).type) && EEG.trials == 1
Ieventrem = setdiff_bc([1:length(EEG.event)], Ievent );
tmpevent = EEG.event;
boundaryindex = eeg_findboundaries(tmpevent(Ieventrem));
if ~isempty(boundaryindex)
boundaryindex = Ieventrem(boundaryindex);
Ievent = [ Ievent boundaryindex ];
end
Ievent = sort(Ievent);
else
boundaryindex = [];
end
else
boundaryindex = [];
end
% rename events if necessary
% --------------------------
if ~isempty(g.renametype)
fprintf('Pop_selectevent: renaming %d selected events (out of %d)\n', length(Ievent), length(EEG.event));
if ~isempty(g.oldtypefield)
for index = setdiff_bc(Ievent, boundaryindex)
eval([ 'EEG.event(index).' g.oldtypefield '= EEG.event(index).type;']);
EEG.event(index).type = g.renametype;
end
else
for index = setdiff_bc(Ievent, boundaryindex)
EEG.event(index).type = g.renametype;
end
end
end
% Events: delete epochs
% ---------------------
if strcmp( lower(g.deleteepochs), 'on') && EEG.trials > 1
% ask for confirmation
% --------------------
Iepoch = ones(1, EEG.trials);
for index = 1:length(Ievent)
Iepoch(EEG.event(Ievent(index)).epoch) = 0;
end
if strcmpi(g.invertepochs, 'on')
Iepoch = ~Iepoch;
end
Iepoch = find(Iepoch == 0);
if length(Iepoch) == 0,
error('Empty dataset: all epochs have been removed');
end
if nargin < 2
ButtonName=questdlg2(strvcat([ 'Warning: keep ' num2str(length(Iepoch)) ' epochs (delete ' num2str(EEG.trials-length(Iepoch)) ...
' unreferenced epochs)' ]), ...
'Confirmation', ...
'Cancel', 'Ok','Ok');
else ButtonName = 'ok'; end
switch lower(ButtonName),
case 'cancel', return;
case 'ok',
if strcmpi(g.deleteevents, 'on')
EEG.event = EEG.event(Ievent);
end
EEG = pop_select(EEG, 'trial', Iepoch);
end % switch
else
% delete events if necessary
% --------------------------
if strcmpi(g.deleteevents, 'on')
EEG.event = EEG.event(Ievent);
end
end
EEG = eeg_checkset(EEG, 'eventconsistency');
% generate the output command
% ---------------------------
argsout = {};
for index =1:2:length(args)
if ~isempty(args{index+1})
argsout = { argsout{:} args{index} args{index+1}};
end
end
com = sprintf('EEG = pop_selectevent( EEG, %s);', vararg2str(argsout));
% chop the text so that it fits into the description window
% ---------------------------------------------------------
function chopedtext = choptext( tmptext )
chopedtext = '';
while length(tmptext) > 30
blanks = findstr( tmptext, ' ');
[tmp I] = min( abs(blanks - 30) );
chopedtext = [ chopedtext ''' 10 ''' tmptext(1:blanks(I)) ];
tmptext = tmptext(blanks(I)+1:end);
end;
chopedtext = [ chopedtext ''' 10 ''' tmptext];
chopedtext = chopedtext(7:end);
return;
% ======== JRI BUGFIX 3/6/14
% safely concatenate a numeric field that may contain empty values, replacing them with nans
% ---------------------------------------------------------
function tmpvarvalue = safeConcatenate(S, fieldname)
try
tmpvarvalue = {S.(fieldname)};
tmpvarvalue = cellfun(@(x) fastif(isempty(x),nan,x), tmpvarvalue);
catch
%keep this style for backwards compatibility?
eval( [ 'tmpvarvalue = {S(:).' fieldname ' };'] );
for itmp = 1:length(tmpvarvalue),
if isempty(tmpvarvalue{itmp}),
tmpvarvalue{itmp} = nan;
end
end
tmpvarvalue = cell2mat(tmpvarvalue);
end
% ======== JRI BUGFIX 3/6/14