forked from fieldtrip/fieldtrip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_databrowser.m
1862 lines (1650 loc) · 74.1 KB
/
ft_databrowser.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function [cfg] = ft_databrowser(cfg, data)
% FT_DATABROWSER can be used for visual inspection of data. Artifacts that
% were detected by artifact functions (see FT_ARTIFACT_xxx functions where
% xxx is the type of artifact) are marked. Additionally data pieces can be
% marked and unmarked as artifact by manual selection. The output cfg
% contains the updated specification of the artifacts. When visually selection
% data, a right-click will bring up a context-menu containing functions to be
% executed on the selected data. You can use your own function using cfg.selfun
% and cfg.selcfg. You can use multiple functions by giving the names/cfgs as a
% cell-array.
%
% Use as
% cfg = ft_databrowser(cfg)
% where the configuration structure contains the reference to the dataset
% on your hard disk (see below), or use as
% cfg = ft_databrowser(cfg, data)
% where the input data is a structure as obtained from FT_PREPROCESSING or
% from FT_COMPONENTANALYSIS.
%
% If you want to browse data that is on disk, you have to specify
% cfg.dataset = string with the filename
% Instead of specifying the dataset, you can also explicitely specify the
% name of the file containing the header information and the name of the
% file containing the data, using
% cfg.datafile = string with the filename
% cfg.headerfile = string with the filename
%
% The following configuration options are supported:
% cfg.ylim = vertical scaling, can be 'maxmin', 'maxabs' or [ymin ymax] (default = 'maxabs')
% cfg.zlim = color scaling to apply to component topographies, 'minmax', 'maxabs' (default = 'maxmin')
% cfg.blocksize = duration in seconds for cutting the data up
% cfg.trl = structure that defines the data segments of interest, only applicable for trial-based data
% cfg.continuous = 'yes' or 'no' whether the data should be interpreted as continuous or trial-based
% cfg.channel = cell-array with channel labels, see FT_CHANNELSELECTION
% cfg.plotlabels = 'yes' (default), 'no', 'some'; whether
% to plot channel labels in vertical viewmode ('some' plots one in every ten
% labels; useful when plotting a large number of channels at a time)
% cfg.ploteventlabels = 'type=value', 'colorvalue' (default = 'type=value');
% cfg.viewmode = string, 'butterfly', 'vertical', 'component' for visualizing components e.g. from an ICA (default is 'butterfly')
% cfg.artfctdef.xxx.artifact = Nx2 matrix with artifact segments see FT_ARTIFACT_xxx functions
% cfg.selectfeature = string, name of feature to be selected/added (default = 'visual')
% cfg.selectmode = 'markartifact', 'markpeakevent', 'marktroughevent' (default = 'markartifact')
% cfg.colorgroups = 'sequential' 'allblack' 'labelcharx' (x = xth character in label), 'chantype' or
% vector with length(data/hdr.label) defining groups (default = 'sequential')
% cfg.channelcolormap = COLORMAP (default = customized lines map with 15 colors)
% cfg.selfun = string, name of function which is evaluated using the right-click context menu
% The selected data and cfg.selcfg are passed on to this function.
% cfg.selcfg = configuration options for function in cfg.selfun
% cfg.eegscale = number, scaling to apply to the EEG channels prior to display
% cfg.eogscale = number, scaling to apply to the EOG channels prior to display
% cfg.ecgscale = number, scaling to apply to the ECG channels prior to display
% cfg.emgscale = number, scaling to apply to the EMG channels prior to display
% cfg.megscale = number, scaling to apply to the MEG channels prior to display
% cfg.gradscale = number, scaling to apply to the MEG gradiometer channels prior to display (in addition to the cfg.megscale factor)
% cfg.magscale = number, scaling to apply to the MEG magnetometer channels prior to display (in addition to the cfg.megscale factor)
% cfg.mychanscale = number, scaling to apply to the channels specified in cfg.mychan
% cfg.mychan = Nx1 cell-array with selection of channels
% cfg.chanscale = Nx1 vector with scaling factors, one per channel specified in cfg.channel
% cfg.compscale = string, 'global' or 'local', defines whether the colormap for the topographic scaling is
% applied per topography or on all visualized components (default 'global')
%
% In case of component viewmode, a layout is required. If no layout is
% give, an attempt is made to construct one from the sensor definition.
% EEG or MEG sensor positions can be present in the data or can be specified as
% cfg.elec = structure with electrode positions, see FT_DATATYPE_SENS
% cfg.grad = structure with gradiometer definition, see FT_DATATYPE_SENS
% cfg.elecfile = name of file containing the electrode positions, see FT_READ_SENS
% cfg.gradfile = name of file containing the gradiometer definition, see FT_READ_SENS
%
% The scaling to the EEG, EOG, ECG, EMG and MEG channels is optional and
% can be used to bring the absolute numbers of the different channel types
% in the same range (e.g. fT and uV). The channel types are determined from
% the input data using FT_CHANNELSELECTION.
%
% The "artifact" field in the output cfg is a Nx2 matrix comparable to the
% "trl" matrix of FT_DEFINETRIAL. The first column of which specifying the
% beginsamples of an artifact period, the second column contains the
% endsamples of the artifactperiods.
%
% NOTE for debugging: in case the databrowser crashes, use delete(gcf) to
% kill the figure.
%
% See also FT_PREPROCESSING, FT_REJECTARTIFACT, FT_ARTIFACT_EOG,
% FT_ARTIFACT_MUSCLE, FT_ARTIFACT_JUMP, FT_ARTIFACT_MANUAL,
% FT_ARTIFACT_THRESHOLD, FT_ARTIFACT_CLIP, FT_ARTIFACT_ECG,
% FT_COMPONENTANALYSIS
% Copyright (C) 2009-2011, Robert Oostenveld, Ingrid Nieuwenhuis
%
% This file is part of FieldTrip, see http://www.ru.nl/neuroimaging/fieldtrip
% for the documentation and details.
%
% FieldTrip is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% FieldTrip is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$
% Undocumented options
%
% FIXME these should be removed
% FIXME document these
% cfg.preproc
% cfg.channelcolormap
% cfg.colorgroups
revision = '$Id$';
% do the general setup of the function
ft_defaults
ft_preamble init
ft_preamble provenance
ft_preamble trackconfig
ft_preamble debug
hasdata = (nargin>1);
hascomp = hasdata && ft_datatype(data, 'comp');
% for backward compatibility
cfg = ft_checkconfig(cfg, 'unused', {'comps', 'inputfile', 'outputfile'});
cfg = ft_checkconfig(cfg, 'renamed', {'zscale', 'ylim'});
cfg = ft_checkconfig(cfg, 'renamedval', {'ylim', 'auto', 'maxabs'});
cfg = ft_checkconfig(cfg, 'renamedval', {'selectmode', 'mark', 'markartifact'});
% ensure that the preproc specific options are located in the cfg.preproc substructure
cfg = ft_checkconfig(cfg, 'createsubcfg', {'preproc'});
% set the defaults
if ~isfield(cfg, 'ylim'), cfg.ylim = 'maxabs'; end
if ~isfield(cfg, 'artfctdef'), cfg.artfctdef = struct; end
if ~isfield(cfg, 'selectfeature'), cfg.selectfeature = 'visual'; end % string or cell-array
if ~isfield(cfg, 'selectmode'), cfg.selectmode = 'markartifact'; end
if ~isfield(cfg, 'blocksize'), cfg.blocksize = []; end % now used for both continuous and non-continuous data, defaulting done below
if ~isfield(cfg, 'preproc'), cfg.preproc = []; end % see preproc for options
if ~isfield(cfg, 'selfun'), cfg.selfun = []; end % default functions: 'simpleFFT','multiplotER','topoplotER','topoplotVAR','movieplotER'
if ~isfield(cfg, 'selcfg'), cfg.selcfg = []; end % defaulting done below, requires layouts/etc to be processed
if ~isfield(cfg, 'colorgroups'), cfg.colorgroups = 'sequential'; end
if ~isfield(cfg, 'channelcolormap'), cfg.channelcolormap = [0.75 0 0;0 0 1;0 1 0;0.44 0.19 0.63;0 0.13 0.38;0.5 0.5 0.5;1 0.75 0;1 0 0;0.89 0.42 0.04;0.85 0.59 0.58;0.57 0.82 0.31;0 0.69 0.94;1 0 0.4;0 0.69 0.31;0 0.44 0.75]; end
if ~isfield(cfg, 'eegscale'), cfg.eegscale = []; end
if ~isfield(cfg, 'eogscale'), cfg.eogscale = []; end
if ~isfield(cfg, 'ecgscale'), cfg.ecgscale = []; end
if ~isfield(cfg, 'emgscale'), cfg.emgscale = []; end
if ~isfield(cfg, 'megscale'), cfg.megscale = []; end
if ~isfield(cfg, 'magscale'), cfg.magscale = []; end
if ~isfield(cfg, 'gradscale'), cfg.gradscale = []; end
if ~isfield(cfg, 'chanscale'), cfg.chanscale = []; end
if ~isfield(cfg, 'mychanscale'), cfg.mychanscale = []; end
if ~isfield(cfg, 'layout'), cfg.layout = []; end
if ~isfield(cfg, 'plotlabels'), cfg.plotlabels = 'yes'; end
if ~isfield(cfg, 'event'), cfg.event = []; end % this only exists for backward compatibility and should not be documented
if ~isfield(cfg, 'continuous'), cfg.continuous = []; end % the default is set further down in the code, conditional on the input data
if ~isfield(cfg, 'ploteventlabels'), cfg.ploteventlabels = 'type=value'; end
cfg.zlim = ft_getopt(cfg, 'zlim', 'maxmin');
cfg.compscale = ft_getopt(cfg, 'compscale', 'global');
if ~isfield(cfg, 'viewmode')
% butterfly, vertical, component
if hascomp
cfg.viewmode = 'component';
else
cfg.viewmode = 'butterfly';
end
end
if ~isempty(cfg.chanscale)
if ~isfield(cfg,'channel')
warning('ignoring cfg.chanscale; this should only be used when an explicit channel selection is being made');
cfg.chanscale = [];
elseif numel(cfg.channel) ~= numel(cfg.chanscale)
error('cfg.chanscale should have the same number of elements as cfg.channel');
end
% make sure chanscale is a column vector, not a row vector
if size(cfg.chanscale,2) > size(cfg.chanscale,1)
cfg.chanscale = cfg.chanscale';
end
end
if ~isempty(cfg.mychanscale) && ~isfield(cfg,'mychan')
warning('ignoring cfg.mychanscale; no channels specified in cfg.mychan');
cfg.mychanscale = [];
end
if ~isfield(cfg, 'channel'),
if hascomp
if size(data.topo,2)>9
cfg.channel = 1:10;
else
cfg.channel = 1:size(data.topo,2);
end
else
cfg.channel = 'all';
end
end
if strcmp(cfg.viewmode, 'component')
% read or create the layout that will be used for the topoplots
tmpcfg = [];
tmpcfg.layout = cfg.layout;
if isempty(cfg.layout)
warning('No layout specified - will try to construct one using sensor positions');
if ft_datatype(data, 'meg')
tmpcfg.grad = ft_fetch_sens(cfg, data);
elseif ft_datatype(data, 'eeg')
tmpcfg.elec = ft_fetch_sens(cfg, data);
else
error('cannot infer sensor type');
end
end
cfg.layout = ft_prepare_layout(tmpcfg);
elseif ~isempty(cfg.layout)
tmpcfg = [];
tmpcfg.layout = cfg.layout;
cfg.layout = ft_prepare_layout(tmpcfg);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set the defaults and do some preparation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if hasdata
% save whether data came from a timelock structure
istimelock = strcmp(ft_datatype(data),'timelock');
% check if the input data is valid for this function
data = ft_checkdata(data, 'datatype', {'raw', 'comp'}, 'feedback', 'yes', 'hassampleinfo', 'yes');
% fetch the header from the data structure in memory
hdr = ft_fetch_header(data);
if isfield(data, 'cfg') && ~isempty(ft_findcfg(data.cfg, 'origfs'))
% don't use the events in case the data has been resampled
warning('the data has been resampled, not showing the events');
event = [];
elseif ~isempty(cfg.event)
% use the events that the user passed in the configuration
event = cfg.event;
else
% fetch the events from the data structure in memory
event = ft_fetch_event(data);
end
cfg.channel = ft_channelselection(cfg.channel, hdr.label);
chansel = match_str(data.label, cfg.channel);
Nchans = length(chansel);
if isempty(cfg.continuous)
if numel(data.trial) == 1 && ~istimelock
cfg.continuous = 'yes';
else
cfg.continuous = 'no';
end
else
if strcmp(cfg.continuous, 'yes') && (numel(data.trial) > 1)
warning('interpreting trial-based data as continous, time-axis is no longer appropriate. t(0) now corresponds to the first sample of the first trial, and t(end) to the last sample of the last trial')
end
end
% this is how the input data is segmented
trlorg = zeros(numel(data.trial), 3);
trlorg(:,[1 2]) = data.sampleinfo;
% recreate offset vector (databrowser depends on this for visualisation)
for ntrl = 1:numel(data.trial)
trlorg(ntrl,3) = time2offset(data.time{ntrl}, data.fsample);
end
Ntrials = size(trlorg, 1);
else
% check if the input cfg is valid for this function
cfg = ft_checkconfig(cfg, 'dataset2files', {'yes'});
cfg = ft_checkconfig(cfg, 'required', {'headerfile', 'datafile'});
cfg = ft_checkconfig(cfg, 'renamed', {'datatype', 'continuous'});
cfg = ft_checkconfig(cfg, 'renamedval', {'continuous', 'continuous', 'yes'});
% read the header from file
hdr = ft_read_header(cfg.headerfile, 'headerformat', cfg.headerformat);
if isempty(cfg.continuous)
if hdr.nTrials==1
cfg.continuous = 'yes';
else
cfg.continuous = 'no';
end
end
if ~isempty(cfg.event)
% use the events that the user passed in the configuration
event = cfg.event;
else
% read the events from file
event = ft_read_event(cfg.dataset);
end
cfg.channel = ft_channelselection(cfg.channel, hdr.label);
chansel = match_str(hdr.label, cfg.channel);
Nchans = length(chansel);
if strcmp(cfg.continuous, 'yes')
Ntrials = 1;
else
Ntrials = hdr.nTrials;
end
% FIXME in case of continuous=yes the trl should be [1 hdr.nSamples*nTrials 0]
% and a scrollbar should be used
% construct trl-matrix for data from file on disk
trlorg = zeros(Ntrials,3);
if strcmp(cfg.continuous, 'yes')
trlorg(1, [1 2]) = [1 hdr.nSamples*hdr.nTrials];
else
for k = 1:Ntrials
trlorg(k,[1 2]) = [1 hdr.nSamples] + [hdr.nSamples hdr.nSamples] .* (k-1);
end
end
end % if hasdata
if strcmp(cfg.continuous,'no') && isempty(cfg.blocksize)
cfg.blocksize = (trlorg(1,2) - trlorg(1,1)+1) ./ hdr.Fs;
elseif strcmp(cfg.continuous,'yes') && isempty(cfg.blocksize)
cfg.blocksize = 1;
end
% FIXME make a check for the consistency of cfg.continous, cfg.blocksize, cfg.trl and the data header
if Nchans == 0
error('no channels to display');
end
if Ntrials == 0
error('no trials to display');
end
if ischar(cfg.selectfeature)
% ensure that it is a cell array
cfg.selectfeature = {cfg.selectfeature};
end
if ~isempty(cfg.selectfeature)
for i=1:length(cfg.selectfeature)
if ~isfield(cfg.artfctdef, cfg.selectfeature{i})
cfg.artfctdef.(cfg.selectfeature{i}) = [];
cfg.artfctdef.(cfg.selectfeature{i}).artifact = zeros(0,2);
end
end
end
% determine the vertical scaling
if ischar(cfg.ylim)
if hasdata
% the first trial is used to determine the vertical scaling
dat = data.trial{1}(chansel,:);
else
% one second of data is read from file to determine the vertical scaling
dat = ft_read_data(cfg.datafile, 'header', hdr, 'begsample', 1, 'endsample', round(hdr.Fs), 'chanindx', chansel, 'checkboundary', strcmp(cfg.continuous, 'no'), 'dataformat', cfg.dataformat, 'headerformat', cfg.headerformat);
end % if hasdata
minval = min(dat(:));
maxval = max(dat(:));
switch cfg.ylim
case 'maxabs'
maxabs = max(abs([minval maxval]));
scalefac = 10^(fix(log10(maxabs)));
maxabs = (round(maxabs / scalefac * 100) / 100) * scalefac;
cfg.ylim = [-maxabs maxabs];
case 'maxmin'
cfg.ylim = [minval maxval];
otherwise
error('unsupported value for cfg.ylim');
end % switch ylim
% zoom in a bit when viemode is vertical
if strcmp(cfg.viewmode,'vertical')
cfg.ylim = cfg.ylim/10;
end
else
if (numel(cfg.ylim) ~= 2) || ~isnumeric(cfg.ylim)
error('cfg.ylim needs to be a 1x2 vector [ymin ymax], describing the upper and lower limits')
end
end
% determine coloring of channels
if hasdata
labels_all = data.label;
else
labels_all= hdr.label;
end
if size(cfg.channelcolormap,2) ~= 3
error('cfg.channelcolormap is not valid, size should be Nx3')
end
if isnumeric(cfg.colorgroups)
% groups defined by user
if length(labels_all) ~= length(cfg.colorgroups)
error('length(cfg.colorgroups) should be length(data/hdr.label)')
end
R = cfg.channelcolormap(:,1);
G = cfg.channelcolormap(:,2);
B = cfg.channelcolormap(:,3);
chancolors = [R(cfg.colorgroups(:)) G(cfg.colorgroups(:)) B(cfg.colorgroups(:))];
elseif strcmp(cfg.colorgroups, 'allblack')
chancolors = zeros(length(labels_all),3);
elseif strcmp(cfg.colorgroups, 'chantype')
type = ft_chantype(labels_all);
[tmp1 tmp2 cfg.colorgroups] = unique(type);
fprintf('%3d colorgroups were identified\n',length(tmp1))
R = cfg.channelcolormap(:,1);
G = cfg.channelcolormap(:,2);
B = cfg.channelcolormap(:,3);
chancolors = [R(cfg.colorgroups(:)) G(cfg.colorgroups(:)) B(cfg.colorgroups(:))];
elseif strcmp(cfg.colorgroups(1:9), 'labelchar')
% groups determined by xth letter of label
labelchar_num = str2double(cfg.colorgroups(10));
vec_letters = num2str(zeros(length(labels_all),1));
for iChan = 1:length(labels_all)
vec_letters(iChan) = labels_all{iChan}(labelchar_num);
end
[tmp1 tmp2 cfg.colorgroups] = unique(vec_letters);
fprintf('%3d colorgroups were identified\n',length(tmp1))
R = cfg.channelcolormap(:,1);
G = cfg.channelcolormap(:,2);
B = cfg.channelcolormap(:,3);
chancolors = [R(cfg.colorgroups(:)) G(cfg.colorgroups(:)) B(cfg.colorgroups(:))];
elseif strcmp(cfg.colorgroups, 'sequential')
% no grouping
chancolors = lines(length(labels_all));
else
error('do not understand cfg.colorgroups')
end
% collect the artifacts that have been detected from cfg.artfctdef.xxx.artifact
artlabel = fieldnames(cfg.artfctdef);
sel = zeros(size(artlabel));
artifact = cell(size(artlabel));
for i=1:length(artlabel)
sel(i) = isfield(cfg.artfctdef.(artlabel{i}), 'artifact');
if sel(i)
artifact{i} = cfg.artfctdef.(artlabel{i}).artifact;
fprintf('detected %3d %s artifacts\n', size(artifact{i}, 1), artlabel{i});
end
end
% get the subset of the artfctdef fields that seem to contain artifacts
artifact = artifact(sel==1);
artlabel = artlabel(sel==1);
if length(artlabel) > 9
error('only up to 9 artifacts groups supported')
end
% make artdata representing all artifacts in a "raw data" format
datendsample = max(trlorg(:,2));
artdata = [];
artdata.trial{1} = convert_event(artifact, 'boolvec', 'endsample', datendsample); % every artifact is a "channel"
artdata.time{1} = offset2time(0, hdr.Fs, datendsample);
artdata.label = artlabel;
artdata.fsample = hdr.Fs;
artdata.cfg.trl = [1 datendsample 0];
% determine amount of unique event types (for cfg.ploteventlabels)
if ~isempty(event) && isstruct(event)
eventtypes = unique({event.type});
else
eventtypes = [];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set up default defselfuns
% use two cfg sections
% cfg.selfun - labels that are presented in rightclick menu, and is appended using ft_getuserfun(..., 'browse') later on to create a function handle
% cfg.selcfg - cfgs for functions to be executed
defselfun = [];
defselcfg = [];
% simplefft
defselfun{1} = 'simpleFFT';
tmpcfg = [];
tmpcfg.chancolors = chancolors;
defselcfg{1} = tmpcfg;
% multiplotER
defselfun{2} = 'multiplotER';
tmpcfg = [];
tmpcfg.layout = cfg.layout;
defselcfg{2} = tmpcfg;
% topoplotER
defselfun{3} = 'topoplotER';
tmpcfg = [];
tmpcfg.layout = cfg.layout;
defselcfg{3} = tmpcfg;
% topoplotVAR
defselfun{4} = 'topoplotVAR';
tmpcfg = [];
tmpcfg.layout = cfg.layout;
defselcfg{4} = tmpcfg;
% movieplotER
defselfun{5} = 'movieplotER';
tmpcfg = [];
tmpcfg.layout = cfg.layout;
tmpcfg.interactive = 'yes';
defselcfg{5} = tmpcfg;
% add defselfuns to user-specified defselfuns
if ~iscell(cfg.selfun) && ~isempty(cfg.selfun)
cfg.selfun = {cfg.selfun};
cfg.selfun = [cfg.selfun defselfun];
% do the same for the cfgs
cfg.selcfg = {cfg.selcfg}; % assume the cfg isnt a cell
cfg.selcfg = [cfg.selcfg defselcfg];
else
cfg.selfun = defselfun;
cfg.selcfg = defselcfg;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set up the data structures used in the GUI
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% opt represents the global data/settings, it should contain
% - the original data, epoched or continuous
% - the artifacts represented as continuous data
% - the redraw_cb settings
% - the preproc settings
% - the select_range_cb settings (also used in keyboard_cb)
% these elements are stored inside the figure so that the callback routines can modify them
opt = [];
if hasdata
opt.orgdata = data;
else
opt.orgdata = []; % this means that it will look in cfg.dataset
end
if strcmp(cfg.continuous, 'yes')
opt.trialviewtype = 'segment';
else
opt.trialviewtype = 'trial';
end
opt.artdata = artdata;
opt.hdr = hdr;
opt.event = event;
opt.trlop = 1; % the active trial being displayed
opt.ftsel = find(strcmp(artlabel,cfg.selectfeature)); % current artifact/feature being selected
opt.trlorg = trlorg;
opt.fsample = hdr.Fs;
opt.artcolors = [0.9686 0.7608 0.7686; 0.7529 0.7098 0.9647; 0.7373 0.9725 0.6824;0.8118 0.8118 0.8118; 0.9725 0.6745 0.4784; 0.9765 0.9176 0.5686; 0.6863 1 1; 1 0.6863 1; 0 1 0.6000];
opt.chancolors = chancolors;
opt.cleanup = false; % this is needed for a corrent handling if the figure is closed (either in the corner or by "q")
opt.chanindx = []; % this is used to check whether the component topographies need to be redrawn
opt.eventtypes = eventtypes;
opt.eventtypescolors = [0 0 0; 1 0 0; 0 0 1; 0 1 0; 1 0 1; 0.5 0.5 0.5; 0 1 1; 1 1 0];
opt.eventtypecolorlabels = {'black', 'red', 'blue', 'green', 'cyan', 'grey', 'light blue', 'yellow'};
opt.nanpaddata = []; % this is used to allow horizontal scaling to be constant (when looking at last segment continous data, or when looking at segmented/zoomed-out non-continous data)
opt.trllock = []; % this is used when zooming into trial based data
% save original layout when viewmode = component
if strcmp(cfg.viewmode,'component')
opt.layorg = cfg.layout;
end
% determine labelling of channels
if strcmp(cfg.plotlabels, 'yes')
opt.plotLabelFlag = 1;
elseif strcmp(cfg.plotlabels, 'some')
opt.plotLabelFlag = 2;
else
opt.plotLabelFlag = 0;
end
h = figure;
setappdata(h, 'opt', opt);
setappdata(h, 'cfg', cfg);
% set the figure window title
funcname = mfilename();
if nargin < 2
if isfield(cfg, 'dataset')
dataname = cfg.dataset;
elseif isfield(cfg, 'datafile')
dataname = cfg.datafile;
else
dataname = [];
end
else
dataname = inputname(2);
end
set(gcf, 'Name', sprintf('%d: %s: %s', gcf, funcname, join_str(', ',dataname)));
set(gcf, 'NumberTitle', 'off');
% set zoom option to on
% zoom(h,'on')
% set(zoom(h),'actionPostCallback',@zoom_drawlabels_cb)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set up the figure and callbacks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set(h, 'KeyPressFcn', @keyboard_cb);
set(h, 'WindowButtonDownFcn', {@ft_select_range, 'multiple', false, 'xrange', true, 'yrange', false, 'clear', true, 'contextmenu', cfg.selfun, 'callback', {@select_range_cb, h}, 'event', 'WindowButtonDownFcn'});
set(h, 'WindowButtonUpFcn', {@ft_select_range, 'multiple', false, 'xrange', true, 'yrange', false, 'clear', true, 'contextmenu', cfg.selfun, 'callback', {@select_range_cb, h}, 'event', 'WindowButtonUpFcn'});
set(h, 'WindowButtonMotionFcn', {@ft_select_range, 'multiple', false, 'xrange', true, 'yrange', false, 'clear', true, 'contextmenu', cfg.selfun, 'callback', {@select_range_cb, h}, 'event', 'WindowButtonMotionFcn'});
% make the user interface elements for the data view
uicontrol('tag', 'labels', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', opt.trialviewtype, 'userdata', 't')
uicontrol('tag', 'buttons', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', '<', 'userdata', 'leftarrow')
uicontrol('tag', 'buttons', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', '>', 'userdata', 'rightarrow')
uicontrol('tag', 'labels', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', 'channel','userdata', 'c')
uicontrol('tag', 'buttons', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', '<', 'userdata', 'uparrow')
uicontrol('tag', 'buttons', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', '>', 'userdata', 'downarrow')
uicontrol('tag', 'labels', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', 'horizontal', 'userdata', 'h')
uicontrol('tag', 'buttons', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', '-', 'userdata', 'shift+leftarrow')
uicontrol('tag', 'buttons', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', '+', 'userdata', 'shift+rightarrow')
uicontrol('tag', 'labels', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', 'vertical', 'userdata', 'v')
uicontrol('tag', 'buttons', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', '-', 'userdata', 'shift+downarrow')
uicontrol('tag', 'buttons', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', '+', 'userdata', 'shift+uparrow')
% legend artifacts/features
for iArt = 1:length(artlabel)
uicontrol('tag', 'artifactui', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', artlabel{iArt}, 'userdata', num2str(iArt), 'position', [0.91, 0.9 - ((iArt-1)*0.09), 0.08, 0.04], 'backgroundcolor', opt.artcolors(iArt,:))
uicontrol('tag', 'artifactui', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', '<', 'userdata', ['shift+' num2str(iArt)], 'position', [0.91, 0.855 - ((iArt-1)*0.09), 0.03, 0.04], 'backgroundcolor', opt.artcolors(iArt,:))
uicontrol('tag', 'artifactui', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', '>', 'userdata', ['control+' num2str(iArt)], 'position', [0.96, 0.855 - ((iArt-1)*0.09), 0.03, 0.04], 'backgroundcolor', opt.artcolors(iArt,:))
end
if strcmp(cfg.viewmode, 'butterfly')
% button to find label of nearest channel to datapoint
uicontrol('tag', 'artifactui', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', 'identify', 'userdata', 'i', 'position', [0.91, 0.1, 0.08, 0.05], 'backgroundcolor', [1 1 1])
end
% 'edit preproc'-button
uicontrol('tag', 'preproccfg', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string','preproc cfg','position', [0.91, 0.55 - ((iArt-1)*0.09), 0.08, 0.04],'callback',@preproc_cfg1_cb)
ft_uilayout(h, 'tag', 'labels', 'width', 0.10, 'height', 0.05);
ft_uilayout(h, 'tag', 'buttons', 'width', 0.05, 'height', 0.05);
ft_uilayout(h, 'tag', 'labels', 'style', 'pushbutton', 'callback', @keyboard_cb);
ft_uilayout(h, 'tag', 'buttons', 'style', 'pushbutton', 'callback', @keyboard_cb);
ft_uilayout(h, 'tag', 'artifactui', 'style', 'pushbutton', 'callback', @keyboard_cb);
ft_uilayout(h, 'tag', 'labels', 'retag', 'viewui');
ft_uilayout(h, 'tag', 'buttons', 'retag', 'viewui');
ft_uilayout(h, 'tag', 'viewui', 'BackgroundColor', [0.8 0.8 0.8], 'hpos', 'auto', 'vpos', 0);
definetrial_cb(h);
redraw_cb(h);
% %% Scrollbar
%
% % set initial scrollbar value
% dx = maxtime;
%
% % set scrollbar position
% fig_pos=get(gca,'position');
% scroll_pos=[fig_pos(1) fig_pos(2) fig_pos(3) 0.02];
%
% % define callback
% S=['set(gca,''xlim'',get(gcbo,''value'')+[ ' num2str(mintime) ',' num2str(maxtime) '])'];
%
% % Creating Uicontrol
% s=uicontrol('style','slider',...
% 'units','normalized','position',scroll_pos,...
% 'callback',S,'min',0,'max',0, ...
% 'visible', 'off'); %'value', xmin
% set initial scrollbar value
% dx = maxtime;
%
% % set scrollbar position
% fig_pos=get(gca,'position');
% scroll_pos=[fig_pos(1) fig_pos(2) fig_pos(3) 0.02];
%
% % define callback
% S=['set(gca,''xlim'',get(gcbo,''value'')+[ ' num2str(mintime) ',' num2str(maxtime) '])'];
%
% % Creating Uicontrol
% s=uicontrol('style','slider',...
% 'units','normalized','position',scroll_pos,...
% 'callback',S,'min',0,'max',0, ...
% 'visible', 'off'); %'value', xmin
%initialize postion of plot
% set(gca,'xlim',[xmin xmin+dx]);
if nargout
% wait until the user interface is closed, get the user data with the updated artifact details
set(h, 'CloseRequestFcn', @cleanup_cb);
while ishandle(h)
uiwait(h);
opt = getappdata(h, 'opt');
if opt.cleanup
delete(h);
end
end
% add the updated artifact definitions to the output cfg
for i=1:length(opt.artdata.label)
cfg.artfctdef.(opt.artdata.label{i}).artifact = convert_event(opt.artdata.trial{1}(i,:), 'artifact');
end
% add the updated preproc to the output
try
browsecfg = getappdata(h, 'cfg');
cfg.preproc = browsecfg.preproc;
end
% add the update event to the output cfg
cfg.event = opt.event;
% do the general cleanup and bookkeeping at the end of the function
ft_postamble debug
ft_postamble trackconfig
ft_postamble provenance
ft_postamble previous data
end % if nargout
end % main function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function cleanup_cb(h, eventdata)
opt = getappdata(h, 'opt');
opt.cleanup = true;
setappdata(h, 'opt', opt);
uiresume
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function definetrial_cb(h, eventdata)
opt = getappdata(h, 'opt');
cfg = getappdata(h, 'cfg');
if strcmp(cfg.continuous, 'no')
% when zooming in, lock the trial! one can only go to the next trial when horizontal scaling doesn't segment the data - from ft-meeting: this might be relaxed later on - roevdmei
if isempty(opt.trllock)
opt.trllock = opt.trlop;
end
locktrllen = ((opt.trlorg(opt.trllock,2)-opt.trlorg(opt.trllock,1)+1) ./ opt.fsample);
% if cfg.blocksize is close to the length of the locked trial, set it to that
if (abs(locktrllen-cfg.blocksize) / locktrllen) < 0.1
cfg.blocksize = locktrllen;
end
%%%%%%%%%
% trial is locked, change subdivision of trial
if cfg.blocksize < locktrllen
% lock the trial if it wasn't locked (and thus trlop refers to the actual trial)
if isempty(opt.trllock)
opt.trllock = trlop;
end
% save current position if already
if isfield(opt, 'trlvis')
thissegbeg = opt.trlvis(opt.trlop,1);
end
datbegsample = min(opt.trlorg(opt.trllock,1));
datendsample = max(opt.trlorg(opt.trllock,2));
smpperseg = round(opt.fsample * cfg.blocksize);
begsamples = datbegsample:smpperseg:datendsample;
endsamples = datbegsample+smpperseg-1:smpperseg:datendsample;
offset = (((1:numel(begsamples))-1)*smpperseg) + opt.trlorg(opt.trllock,3);
if numel(endsamples)<numel(begsamples)
endsamples(end+1) = datendsample;
end
trlvis = [];
trlvis(:,1) = begsamples';
trlvis(:,2) = endsamples';
trlvis(:,3) = offset;
% determine length of each trial, and determine the offset with the current requested zoom-level
trllen = (trlvis(:,2) - trlvis(:,1)+1);
sizediff = smpperseg - trllen;
opt.nanpaddata = sizediff;
if isfield(opt, 'trlvis')
% update the current trial counter and try to keep the current sample the same
opt.trlop = nearest(begsamples, thissegbeg);
end
% update trialviewtype
opt.trialviewtype = 'trialsegment';
% update button
set(findobj(get(h,'children'),'string','trial'),'string','segment');
%%%%%%%%%
%%%%%%%%%
% trial is not locked, go to original trial division and zoom out
elseif cfg.blocksize >= locktrllen
trlvis = opt.trlorg;
% set current trlop to locked trial if it was locked before
if ~isempty(opt.trllock)
opt.trlop = opt.trllock;
end
smpperseg = round(opt.fsample * cfg.blocksize);
% determine length of each trial, and determine the offset with the current requested zoom-level
trllen = (trlvis(:,2) - trlvis(:,1)+1);
sizediff = smpperseg - trllen;
opt.nanpaddata = sizediff;
% update trialviewtype
opt.trialviewtype = 'trial';
% update button
set(findobj(get(h,'children'),'string','trialsegment'),'string',opt.trialviewtype);
% release trial lock
opt.trllock = [];
%%%%%%%%%
end
% save trlvis
opt.trlvis = trlvis;
else
% construct a trial definition for visualisation
if isfield(opt, 'trlvis') % if present, remember where we were
thistrlbeg = opt.trlvis(opt.trlop,1);
end
% look at cfg.blocksize and make opt.trl accordingly
datbegsample = min(opt.trlorg(:,1));
datendsample = max(opt.trlorg(:,2));
smpperseg = round(opt.fsample * cfg.blocksize);
begsamples = datbegsample:smpperseg:datendsample;
endsamples = datbegsample+smpperseg-1:smpperseg:datendsample;
if numel(endsamples)<numel(begsamples)
endsamples(end+1) = datendsample;
end
trlvis = [];
trlvis(:,1) = begsamples';
trlvis(:,2) = endsamples';
% compute the offset. In case if opt.trlorg has multiple trials, the first sample is t=0, otherwise use the offset in opt.trlorg
if size(opt.trlorg,1)==1
offset = begsamples - repmat(begsamples(1),[1 numel(begsamples)]); % offset for all segments compared to the first
offset = offset + opt.trlorg(1,3);
trlvis(:,3) = offset;
else
offset = begsamples - repmat(begsamples(1),[1 numel(begsamples)]);
trlvis(:,3) = offset;
end
if isfield(opt, 'trlvis')
% update the current trial counter and try to keep the current sample the same
% opt.trlop = nearest(round((begsamples+endsamples)/2), thissample);
opt.trlop = nearest(begsamples, thistrlbeg);
end
opt.trlvis = trlvis;
% NaN-padding when horizontal scaling is bigger than the data
% two possible situations, 1) zoomed out so far that all data is one segment, or 2) multiple segments but last segment is smaller than the rest
sizediff = smpperseg-(endsamples-begsamples+1);
opt.nanpaddata = sizediff;
end % if continuous
setappdata(h, 'opt', opt);
setappdata(h, 'cfg', cfg);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function help_cb(h, eventdata)
fprintf('------------------------------------------------------------------------------------\n')
fprintf('You can use the following keyboard buttons in the databrowser\n')
fprintf('1-9 : select artifact type 1-9\n');
fprintf('shift 1-9 : select previous artifact of type 1-9\n');
fprintf(' (does not work with numpad keys)\n');
fprintf('control 1-9 : select next artifact of type 1-9\n');
fprintf('alt 1-9 : select next artifact of type 1-9\n');
fprintf('arrow-left : previous trial\n');
fprintf('arrow-right : next trial\n');
fprintf('shift arrow-up : increase vertical scaling\n');
fprintf('shift arrow-down : decrease vertical scaling\n');
fprintf('shift arrow-left : increase horizontal scaling\n');
fprintf('shift arrow-down : decrease horizontal scaling\n');
fprintf('s : toggles between cfg.selectmode options\n');
fprintf('q : quit\n');
fprintf('------------------------------------------------------------------------------------\n')
fprintf('\n')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function select_range_cb(h, range, cmenulab) %range 1X4 in sec relative to current trial
opt = getappdata(h, 'opt');
cfg = getappdata(h, 'cfg');
% the range should be in the displayed box
range(1) = max(opt.hpos-opt.width/2, range(1));
range(2) = max(opt.hpos-opt.width/2, range(2));
range(1) = min(opt.hpos+opt.width/2, range(1));
range(2) = min(opt.hpos+opt.width/2, range(2));
range = (range-(opt.hpos-opt.width/2)) / opt.width; % left side of the box becomes 0, right side becomes 1
range = range * (opt.hlim(2) - opt.hlim(1)) + opt.hlim(1); % 0 becomes hlim(1), 1 becomes hlim(2)
begsample = opt.trlvis(opt.trlop,1);
endsample = opt.trlvis(opt.trlop,2);
offset = opt.trlvis(opt.trlop,3);
% determine the selection
begsel = round(range(1)*opt.fsample+begsample-offset-1);
endsel = round(range(2)*opt.fsample+begsample-offset);
% artifact selection is now always based on begsample/endsample/offset
% -roevdmei
% the selection should always be confined to the current trial
begsel = max(begsample, begsel);
endsel = min(endsample, endsel);
% mark or execute selfun
if isempty(cmenulab)
% the left button was clicked INSIDE a selected range, update the artifact definition or event
if strcmp(cfg.selectmode, 'markartifact')
% mark or unmark artifacts
artval = opt.artdata.trial{1}(opt.ftsel, begsel:endsel);
artval = any(artval,1);
if any(artval)
fprintf('there is overlap with the active artifact (%s), disabling this artifact\n',opt.artdata.label{opt.ftsel});
opt.artdata.trial{1}(opt.ftsel, begsel:endsel) = 0;
else
fprintf('there is no overlap with the active artifact (%s), marking this as a new artifact\n',opt.artdata.label{opt.ftsel});
opt.artdata.trial{1}(opt.ftsel, begsel:endsel) = 1;
end
% redraw only when marking (so the focus doesn't go back to the databrowser after calling selfuns
setappdata(h, 'opt', opt);
setappdata(h, 'cfg', cfg);
redraw_cb(h);
elseif strcmp(cfg.selectmode, 'markpeakevent') || strcmp(cfg.selectmode, 'marktroughevent')
%mark or unmark events, marking at peak/trough of window
if any(intersect(begsel:endsel, [opt.event.sample]))
fprintf('there is overlap with one or more event(s), disabling this/these event(s)\n');
ind_rem = intersect(begsel:endsel, [opt.event.sample]);
for iRemove = 1:length(ind_rem)
opt.event([opt.event.sample]==ind_rem(iRemove)) = [];
end
else
fprintf('there is no overlap with any event, adding an event to the peak/trough value\n');
% check if only 1 chan, other wise not clear max in which channel. %
% ingnie: would be cool to add the option to select the channel when multiple channels
if size(opt.curdat.trial{1},1) > 1
error('cfg.selectmode = ''markpeakevent'' and ''marktroughevent'' only supported with 1 channel in the data')
end
if strcmp(cfg.selectmode, 'markpeakevent')
[dum ind_minmax] = max(opt.curdat.trial{1}(begsel-begsample+1:endsel-begsample+1));
val = 'peak';
elseif strcmp(cfg.selectmode, 'marktroughevent')
[dum ind_minmax] = min(opt.curdat.trial{1}(begsel-begsample+1:endsel-begsample+1));
val = 'trough';
end
samp_minmax = begsel + ind_minmax - 1;
event_new.type = 'ft_databrowser_manual';
event_new.sample = samp_minmax;
event_new.value = val;
event_new.duration = 1;
event_new.offset = 0;
% add new event to end opt.event
% check if events are in order now
if min(diff([opt.event.sample]))>0
% add new event in line with old ones
nearest_event = nearest([opt.event.sample], samp_minmax);
if opt.event(nearest_event).sample > samp_minmax
%place new event before nearest
ind_event_new = nearest_event;
else
%place new event after nearest
ind_event_new = nearest_event +1;
end
event_lastpart = opt.event(ind_event_new:end);
opt.event(ind_event_new) = event_new;
opt.event(ind_event_new+1:end+1) = event_lastpart;
else
%just add to end