forked from fieldtrip/fieldtrip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_databrowser.m
2407 lines (2141 loc) · 105 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.
%
% Use as
% [cfg] = ft_databrowser(cfg)
% [cfg] = ft_databrowser(cfg, data)
% If you only specify the configuration structure, it should contain the name of the
% dataset on your hard disk (see below). If you specify input data, it should be a
% data 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 continuous data in segments
% 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.allowoverlap = 'yes' or 'no', whether data that is overlapping in multiple trials is allowed (default = 'no')
% cfg.channel = cell-array with channel labels, see FT_CHANNELSELECTION
% cfg.channelclamped = cell-array with channel labels, that when using the 'vertical' viewmode will always be shown at the bottom. This is useful for showing ECG/EOG channels along with the other channels
% cfg.compscale = string, 'global' or 'local', defines whether the colormap for the topographic scaling is applied per topography or on all visualized components (default = 'local')
% cfg.viewmode = string, 'vertical', 'butterfly', or 'component' for visualizing ICA/PCA topographies together with the timecourses (default = 'vertical')
% cfg.plotlabels = 'yes', 'no' or 'some', whether to plot channel labels in vertical viewmode. The option 'some' plots one label for every ten channels, which is useful if there are many channels (default = 'some')
% cfg.plotevents = 'no' or 'yes', whether to plot event markers (default = 'yes')
% cfg.ploteventlabels = 'type=value', 'colorvalue' (default = 'type=value')
% 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', 'labelcharN' (N = Nth character in label), 'chantype' or a vector with the length of the number of channels defining the groups (default = 'sequential')
% cfg.linecolor = string with line colors or Nx3 color map (default = customized lines map with 15 colors)
% cfg.linewidth = linewidth in points (default = 0.5)
% cfg.linestyle = linestyle/marker type, see options of the PLOT function (default = '-')
% cfg.verticalpadding = number or 'auto', padding to be added to top and bottom of plot to avoid channels largely dissappearing when viewmode = 'vertical'/'component' (default = 'auto'). The padding is expressed as a proportion of the total height added to the top and bottom. The setting 'auto' determines the padding depending on the number of channels that are being plotted.
% cfg.selfun = string, name of function that 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.seldat = 'selected' or 'all', specifies whether only the currently selected or all channels will be passed to the selfun (default = 'selected')
% cfg.visible = string, 'on' or 'off' whether figure will be visible (default = 'on')
% cfg.position = location and size of the figure, specified as a vector of the form [left bottom width height]
% cfg.renderer = string, 'opengl', 'zbuffer', 'painters', see MATLAB Figure Properties. If this function crashes, you should try 'painters'.
% cfg.colormap = string, or Nx3 matrix, see FT_COLORMAP
%
% The following options for the scaling of the EEG, EOG, ECG, EMG, MEG and NIRS 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.
% 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.nirsscale = number, scaling to apply to the NIRS channels prior to display
% 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
%
% You can specify preprocessing options that are to be applied to the data prior to
% display. Most options from FT_PREPROCESSING are supported. They should be specified
% in the sub-structure cfg.preproc like these examples
% cfg.preproc.lpfilter = 'no' or 'yes' lowpass filter (default = 'no')
% cfg.preproc.lpfreq = lowpass frequency in Hz
% cfg.preproc.demean = 'no' or 'yes', whether to apply baseline correction (default = 'no')
% cfg.preproc.detrend = 'no' or 'yes', remove linear trend from the data (done per trial) (default = 'no')
% cfg.preproc.baselinewindow = [begin end] in seconds, the default is the complete trial (default = 'all')
%
% In case of component viewmode, a layout is required. If no layout is specified, an
% attempt is made to construct one from the sensor definition that is present in the
% data or specified in the configuration.
% cfg.layout = filename of the layout, see FT_PREPARE_LAYOUT
% cfg.elec = structure with electrode positions or filename, see FT_READ_SENS
% cfg.grad = structure with gradiometer definition or filename, see FT_READ_SENS
% Additional plotting options for the component viewmode:
% cfg.gridscale = scalar, number of points along both directions for interpolation (default = 45 here)
% cfg.shading = string, 'none', 'flat', 'interp' (default = 'flat')
% cfg.interplimits = string, 'electrodes' or 'mask' (default here = 'mask')
% cfg.interpolation = string, 'nearest', 'linear', 'natural', 'cubic' or 'v4' (default = 'v4')
% cfg.contournum = topoplot contour lines
%
% The default font size might be too small or too large, depending on the number of
% channels. You can use the following options to change the size of text inside the
% figure and along the axes.
% cfg.fontsize = number, fontsize inside the figure (default = 0.03)
% cfg.fontunits = string, can be 'normalized', 'points', 'pixels', 'inches' or 'centimeters' (default = 'normalized')
% cfg.axisfontsize = number, fontsize along the axes (default = 10)
% cfg.axisfontunits = string, can be 'normalized', 'points', 'pixels', 'inches' or 'centimeters' (default = 'points')
%
% 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.
%
% In butterfly and vertical mode, you can use the "identify" button to reveal the name of a
% channel. Please be aware that it searches only vertically. This means that it will
% return the channel with the amplitude closest to the point you have clicked at the
% specific time point. This might be counterintuitive at first.
%
% The "cfg.artfctdef" structure in the output cfg is comparable to the configuration
% used by the artifact detection functions like FT_ARTIFACT_ZVALUE and in
% FT_REJECTARTIFACT. It contains for each artifact type an Nx2 matrix in which the
% first column corresponds to the begin samples of an artifact period, the second
% column contains the end samples of the artifact periods.
%
% In case the databrowser crashes and you cannot close the window, use delete(gcf) to
% get rid of 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-2022, Robert Oostenveld, Ingrid Nieuwenhuis
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% 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$
% these are used by the ft_preamble/ft_postamble function and scripts
ft_revision = '$Id$';
ft_nargin = nargin;
ft_nargout = nargout;
% do the general setup of the function
ft_defaults
ft_preamble init
ft_preamble debug
ft_preamble loadvar data
ft_preamble provenance data
% the ft_abort variable is set to true or false in ft_preamble_init
if ft_abort
return
end
% the data can be passed as input arguments or can be read from disk
hasdata = exist('data', 'var');
hascomp = hasdata && ft_datatype(data, 'comp'); % can be 'raw+comp' or 'timelock+comp'
% check if the input cfg is valid for this function
cfg = ft_checkconfig(cfg, 'forbidden', {'channels'}); % prevent accidental typos, see issue 1729
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'});
cfg = ft_checkconfig(cfg, 'renamedval', {'ploteventlabels', 'colorvalue', 'value'});
cfg = ft_checkconfig(cfg, 'renamed', {'elecfile', 'elec'});
cfg = ft_checkconfig(cfg, 'renamed', {'gradfile', 'grad'});
cfg = ft_checkconfig(cfg, 'renamed', {'optofile', 'opto'});
cfg = ft_checkconfig(cfg, 'renamed', {'channelcolormap', 'linecolor'});
cfg = ft_checkconfig(cfg, 'renamed', {'anonimize', 'anonymize'}); % fix typo in previous version of the code
cfg = ft_checkconfig(cfg, 'renamed', {'anonymise', 'anonymize'}); % use North American and Oxford British spelling
cfg = ft_checkconfig(cfg, 'renamed', {'newfigure', 'figure'});
cfg = ft_checkconfig(cfg, 'deprecated', {'selectfeature'}); % please specify cfg.artfctdef.xxx and cfg.artfctdef.yyy for each feature
% ensure that the preproc specific options are located in the cfg.preproc substructure
cfg = ft_checkconfig(cfg, 'createsubcfg', {'preproc'});
% set the defaults
cfg.ylim = ft_getopt(cfg, 'ylim', 'maxabs');
cfg.zlim = ft_getopt(cfg, 'zlim', 'maxmin');
cfg.artfctdef = ft_getopt(cfg, 'artfctdef', struct);
cfg.selectmode = ft_getopt(cfg, 'selectmode', 'markartifact');
cfg.blocksize = ft_getopt(cfg, 'blocksize'); % now used for both continuous and non-continuous data, defaulting done below
cfg.preproc = ft_getopt(cfg, 'preproc'); % see preproc for options
cfg.selfun = ft_getopt(cfg, 'selfun'); % default functions are 'simpleFFT', 'multiplotER', 'topoplotER', 'topoplotVAR', 'movieplotER'
cfg.selcfg = ft_getopt(cfg, 'selcfg'); % defaulting done below, requires layouts/etc to be processed
cfg.seldat = ft_getopt(cfg, 'seldat', 'current');
cfg.colorgroups = ft_getopt(cfg, 'colorgroups', 'sequential');
cfg.linecolor = ft_getopt(cfg, 'linecolor', []); % the default is defined in lineattributes_common
cfg.linestyle = ft_getopt(cfg, 'linestyle', '-');
cfg.linewidth = ft_getopt(cfg, 'linewidth', 0.5);
cfg.eegscale = ft_getopt(cfg, 'eegscale');
cfg.eogscale = ft_getopt(cfg, 'eogscale');
cfg.ecgscale = ft_getopt(cfg, 'ecgscale');
cfg.emgscale = ft_getopt(cfg, 'emgscale');
cfg.megscale = ft_getopt(cfg, 'megscale');
cfg.magscale = ft_getopt(cfg, 'magscale');
cfg.gradscale = ft_getopt(cfg, 'gradscale');
cfg.chanscale = ft_getopt(cfg, 'chanscale');
cfg.mychanscale = ft_getopt(cfg, 'mychanscale');
cfg.mychan = ft_getopt(cfg, 'mychan');
cfg.layout = ft_getopt(cfg, 'layout');
cfg.colormap = ft_getopt(cfg, 'colormap', 'default');
cfg.plotlabels = ft_getopt(cfg, 'plotlabels', 'some');
cfg.event = ft_getopt(cfg, 'event'); % this only exists for backward compatibility and should not be documented
cfg.continuous = ft_getopt(cfg, 'continuous'); % the default is set further down in the code, conditional on the input data
cfg.precision = ft_getopt(cfg, 'precision', 'double');
cfg.compscale = ft_getopt(cfg, 'compscale', 'local');
cfg.renderer = ft_getopt(cfg, 'renderer');
cfg.fontsize = ft_getopt(cfg, 'fontsize', 12);
cfg.fontunits = ft_getopt(cfg, 'fontunits', 'points'); % inches, centimeters, normalized, points, pixels
cfg.editfontsize = ft_getopt(cfg, 'editfontsize', 12);
cfg.editfontunits = ft_getopt(cfg, 'editfontunits', 'points'); % inches, centimeters, normalized, points, pixels
cfg.axisfontsize = ft_getopt(cfg, 'axisfontsize', 10);
cfg.axisfontunits = ft_getopt(cfg, 'axisfontunits', 'points'); % inches, centimeters, normalized, points, pixels
cfg.verticalpadding = ft_getopt(cfg, 'verticalpadding', 'auto');
cfg.allowoverlap = ft_getopt(cfg, 'allowoverlap', 'no'); % for ft_fetch_data
cfg.contournum = ft_getopt(cfg, 'contournum', 0); % topoplot contour lines
cfg.trl = ft_getopt(cfg, 'trl');
cfg.gridscale = ft_getopt(cfg, 'gridscale', 45);
cfg.shading = ft_getopt(cfg, 'shading', 'flat');
cfg.interplimits = ft_getopt(cfg, 'interplim', 'mask');
cfg.interpolation = ft_getopt(cfg, 'interpmethod', 'v4');
cfg.channelclamped = ft_getopt(cfg, 'channelclamped');
% set the defaults for plotting the events
cfg.plotevents = ft_getopt(cfg, 'plotevents', 'yes');
cfg.ploteventlabels = ft_getopt(cfg, 'ploteventlabels', 'type=value');
cfg.eventalpha = ft_getopt(cfg, 'artifactalpha', 0.2); % for the opacity of events
% set the defaults for plotting the artifacts
cfg.plotartifacts = ft_getopt(cfg, 'plotartifacts', 'yes');
cfg.plotartifactlabels = ft_getopt(cfg, 'plotartifactlabels', '');
cfg.artifactalpha = ft_getopt(cfg, 'artifactalpha', 0.2); % for the opacity of artifacts
% add some defaults for preprocessing, none of these is active but they will show up with the cfg.preproc button in the user interface
cfg.preproc.demean = ft_getopt(cfg.preproc, 'demean', 'no');
cfg.preproc.lpfilter = ft_getopt(cfg.preproc, 'lpfilter', 'no');
cfg.preproc.lpfreq = ft_getopt(cfg.preproc, 'lpfreq', 30);
cfg.preproc.hpfilter = ft_getopt(cfg.preproc, 'hpfilter', 'no');
cfg.preproc.hpfreq = ft_getopt(cfg.preproc, 'hpfreq', 0.5);
% construct the low-level options as key-value pairs, these are passed to FT_READ_HEADER
headeropt = {};
headeropt = ft_setopt(headeropt, 'headerformat', ft_getopt(cfg, 'headerformat')); % is passed to low-level function, empty implies autodetection
headeropt = ft_setopt(headeropt, 'readbids', ft_getopt(cfg, 'readbids')); % is passed to low-level function
headeropt = ft_setopt(headeropt, 'coordsys', ft_getopt(cfg, 'coordsys', 'head')); % is passed to low-level function
headeropt = ft_setopt(headeropt, 'coilaccuracy', ft_getopt(cfg, 'coilaccuracy')); % is passed to low-level function
headeropt = ft_setopt(headeropt, 'checkmaxfilter', ft_getopt(cfg, 'checkmaxfilter')); % this allows to read non-maxfiltered neuromag data recorded with internal active shielding
headeropt = ft_setopt(headeropt, 'chantype', ft_getopt(cfg, 'chantype', {})); % 2017.10.10 AB required for NeuroOmega files
% construct the low-level options as key-value pairs, these are passed to FT_READ_EVENT
eventopt = {};
eventopt = ft_setopt(eventopt, 'eventformat', ft_getopt(cfg, 'eventformat')); % is passed to low-level function, empty implies autodetection
eventopt = ft_setopt(eventopt, 'readbids', ft_getopt(cfg, 'readbids')); % is passed to low-level function
if isempty(ft_getopt(cfg, 'viewmode'))
% can be 'butterfly', 'vertical', or 'component'
if hascomp
cfg.viewmode = 'component';
else
cfg.viewmode = 'vertical';
end
end
if isempty(ft_getopt(cfg, 'colorgroups'))
% can be 'sequential', 'allblack', 'labelcharN', 'chantype', or a vector with the length of the number of channels defining the groups
if hascomp
cfg.colorgroups = 'allblack';
else
cfg.colorgroups = 'sequential';
end
end
if ~isempty(cfg.chanscale)
if ~isfield(cfg, 'channel')
ft_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)
ft_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
cfg.chanscale = cfg.chanscale(:);
end
if ~isempty(cfg.mychanscale) && ~isfield(cfg, 'mychan')
ft_warning('ignoring cfg.mychanscale; no channels specified in cfg.mychan');
cfg.mychanscale = [];
end
if isempty(ft_getopt(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 topographic layout that will be used for the topoplots
tmpcfg = keepfields(cfg, {'layout', 'rows', 'columns', 'commentpos', 'skipcomnt', 'scalepos', 'skipscale', 'projection', 'viewpoint', 'rotate', 'width', 'height', 'elec', 'grad', 'opto', 'showcallinfo', 'trackcallinfo', 'trackusage', 'trackdatainfo', 'trackmeminfo', 'tracktimeinfo'});
if hasdata
% select those channels from the layout that are relevant for the
% decomposition that is being plotted
tmpcfg.channel = data.topolabel;
cfg.layout = ft_prepare_layout(tmpcfg, data);
else
cfg.layout = ft_prepare_layout(tmpcfg);
end
end
if isempty(fieldnames(cfg.artfctdef)) % note that isempty(struct()) returns false
% by default allow the user to specify visual artifacts
cfg.artfctdef.visual.artifact = zeros(0,2);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set the defaults and do some preparation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if hasdata
% save whether data came from a timelock structure
istimelock = strcmp(ft_datatype(data), 'timelock');
if ~isempty(cfg.trl)
% reduce the input data to the requested trial segments
tmpcfg = keepfields(cfg, {'trl'});
data = ft_redefinetrial(tmpcfg, data);
[cfg, data] = rollback_provenance(cfg, data);
end
% check if the input data is valid for this function
data = ft_checkdata(data, 'datatype', {'raw+comp', 'raw'}, '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
ft_warning('the data has been resampled, not showing the events');
event = [];
elseif istimelock
% don't use the events in case the data has been averaged
ft_warning('the data has been averaged, 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)
ft_warning('interpreting trial-based data as continuous, the time axis now corresponds to the continuous data with the first sample being t=0')
end
end
% this is how the input data is segmented
trlorg = sampleinfo2trl(data);
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, headeropt{:});
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, eventopt{:});
end
cfg.channel = ft_channelselection(cfg.channel, hdr.label);
chansel = match_str(hdr.label, cfg.channel);
Nchans = length(chansel);
if ~isfield(cfg, 'trl') || isempty(cfg.trl)
% treat the data as continuous if possible, otherwise define all trials as indicated in the header
if strcmp(cfg.continuous, 'yes')
trlorg = zeros(1, 3);
trlorg(1,1) = 1;
trlorg(1,2) = hdr.nSamples*hdr.nTrials;
trlorg(1,3) = -hdr.nSamplesPre;
else
trlorg = zeros(hdr.nTrials, 3);
for i=1:hdr.nTrials
trlorg(i,1) = (i-1)*hdr.nSamples + 1;
trlorg(i,2) = (i )*hdr.nSamples ;
trlorg(i,3) = -hdr.nSamplesPre;
end
end
elseif ischar(cfg.trl)
% load the trial information from file
trlorg = loadvar(cfg.trl, 'trl');
else
trlorg = cfg.trl;
end
end % if hasdata
% the code below expects an Nx3 matrix with begsample, endsample and offset
if istable(trlorg)
trlorg = table2array(trlorg(:,1:3));
else
trlorg = trlorg(:,1:3);
end
Ntrials = size(trlorg, 1);
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
if cfg.blocksize<round(10*1/hdr.Fs)
ft_warning('the blocksize is very small given the samping rate, increasing blocksize to 10 samples');
cfg.blocksize = round(10*1/hdr.Fs);
end
% FIXME make a check for the consistency of cfg.continuous, cfg.blocksize, cfg.trl and the data header
if Nchans == 0
ft_error('no channels to display');
end
if Ntrials == 0
ft_error('no trials to display');
end
% determine the vertical scaling
if ischar(cfg.ylim)
if hasdata
sel = 1;
while all(isnan(reshape(data.trial{sel}(chansel,:),[],1)))
sel = sel+1;
end
% the first trial is used to determine the vertical scaling
dat = data.trial{sel}(chansel,:);
else
% read one second (or one block) of data to determine the vertical scaling
begsample = 1;
endsample = min(round(hdr.Fs), hdr.nSamples);
dat = ft_read_data(cfg.datafile, 'header', hdr, 'begsample', begsample, 'endsample', endsample, 'chanindx', chansel, 'checkboundary', strcmp(cfg.continuous, 'no'), 'dataformat', cfg.dataformat, headeropt{:});
end % if hasdata
% convert the data to another numeric precision, i.e. double, single or int32
if ~isempty(cfg.precision)
dat = cast(dat, cfg.precision);
end
minval = min(dat(:));
maxval = max(dat(:));
switch cfg.ylim
case 'maxabs'
maxabs = max(abs([minval maxval]));
scalefac = 10^(fix(log10(maxabs)));
if scalefac==0
% this happens if the data is all zeros
scalefac=1;
end
maxabs = (round(maxabs / scalefac * 100) / 100) * scalefac;
cfg.ylim = [-maxabs maxabs];
case 'maxmin'
if minval==maxval
% this happens if the data is constant, e.g. all zero or clipping
minval = minval - eps;
maxval = maxval + eps;
end
cfg.ylim = [minval maxval];
otherwise
ft_error('unsupported value for cfg.ylim');
end % switch ylim
if strcmp(cfg.viewmode, 'vertical') || strcmp(cfg.viewmode, 'component')
% it is OK to have some overlap between the traces in vertical and component viewmodes
% but butterfly plots should stay within the min/max boundaries
scale_adjust = 5;
cfg.ylim = cfg.ylim/scale_adjust;
end
else
if (numel(cfg.ylim) ~= 2) || ~isnumeric(cfg.ylim)
ft_error('cfg.ylim needs to be a 1x2 vector [ymin ymax], describing the upper and lower limits')
end
end
% determine the coloring of channels
if hasdata
linecolor = lineattributes_common(cfg, data);
else
linecolor = lineattributes_common(cfg, hdr);
end
% collect the artifacts 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;
ft_info('detected %3d %s artifacts\n', size(artifact{i}, 1), artlabel{i});
end
end
% continue with the subset of artfctdef fields that actually contain artifacts
artifact = artifact(sel==1);
artlabel = artlabel(sel==1);
% make artdata representing all artifacts in a "raw data" format
endsample = max(trlorg(:,2));
artdata = [];
artdata.trial{1} = artifact2boolvec(artifact, 'endsample', endsample); % every artifact is a "channel"
artdata.time{1} = offset2time(0, hdr.Fs, endsample);
artdata.label = artlabel;
artdata.fsample = hdr.Fs;
artdata.cfg.trl = [1 endsample 0];
% determine the unique artifact types and corresponding colors, this only needs to be done once
artifacttypes = artlabel;
artifactcolors = colorcheck([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], numel(artdata.label));
% determine the unique event types and corresponding colors, this only needs to be done once
if ~isempty(event) && isstruct(event)
eventtypes = unique({event.type});
eventcolors = colorcheck('krbgmcy', numel(eventtypes));
% durations and offsets can be either empty or should be numeric values, see FT_READ_EVENT
% the code further down expects them to be numeric values, so change them to zero
for i=1:numel(event)
if isempty(event(i).duration)
event(i).duration = 0;
end
if isempty(event(i).offset)
event(i).offset = 0;
end
end
else
eventtypes = {};
eventcolors = '';
end
ft_info('the different artifact types correspond to the following colors:');
tmp = pad(artifacttypes); % pad the artifacttypes to the same width
for i=1:length(artifacttypes)
ft_info(' %s = %s\n', tmp{i}, htmlcolors(artifactcolors(i,:)));
end
clear tmp
ft_info('the different event types correspond to the following colors:');
tmp = pad(eventtypes); % pad the eventtypes to the same width
for i=1:length(eventtypes)
ft_info(' %s = %s\n', tmp{i}, htmlcolors(eventcolors(i,:)));
end
clear tmp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set up default functions to be available in the right-click menu
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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
if ~isempty(cfg.selfun) || ~isempty(cfg.selcfg)
if ischar(cfg.selfun)
cfg.selfun = {cfg.selfun};
end
if isstruct(cfg.selcfg)
cfg.selcfg = {cfg.selcfg};
end
elseif isempty(cfg.selfun) && isempty(cfg.selcfg)
% simplefft
cfg.selcfg{1} = [];
cfg.selcfg{1}.linecolor = linecolor;
cfg.selfun{1} = 'simpleFFT';
% multiplotER
cfg.selcfg{2} = [];
cfg.selcfg{2}.linecolor = linecolor;
cfg.selcfg{2}.layout = cfg.layout;
cfg.selcfg{2}.colorgroups = 'sequential';
cfg.selfun{2} = 'multiplotER';
% topoplotER
cfg.selcfg{3} = [];
cfg.selcfg{3}.linecolor = linecolor;
cfg.selcfg{3}.layout = cfg.layout;
cfg.selcfg{3}.colormap = cfg.colormap;
cfg.selfun{3} = 'topoplotER';
% topoplotVAR
cfg.selcfg{4} = [];
cfg.selcfg{4}.layout = cfg.layout;
cfg.selcfg{4}.colormap = cfg.colormap;
cfg.selfun{4} = 'topoplotVAR';
% movieplotER
cfg.selcfg{5} = [];
cfg.selcfg{5}.layout = cfg.layout;
cfg.selcfg{5}.interactive = 'yes';
cfg.selfun{5} = 'movieplotER';
% audiovideo
cfg.selcfg{6} = [];
cfg.selcfg{6}.audiofile = ft_getopt(cfg, 'audiofile');
cfg.selcfg{6}.videofile = ft_getopt(cfg, 'videofile');
cfg.selcfg{6}.anonymize = ft_getopt(cfg, 'anonymize');
cfg.selfun{6} = 'audiovideo';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set up the data structures
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the structure "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 keypress_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 read from cfg.dataset
opt.headeropt = headeropt; % these are passed to FT_READ_HEADER and FT_READ_DATA
end
if strcmp(cfg.continuous, 'yes')
opt.trialviewtype = 'segment';
else
opt.trialviewtype = 'trial';
end
opt.hdr = hdr;
opt.trlop = 1; % the active trial being displayed
opt.artsel = 1; % the currently selected artifact/feature
opt.trlorg = trlorg;
opt.fsample = hdr.Fs;
opt.linecolor = linecolor;
opt.changedchanflg = true; % trigger for redrawing channel labels and preparing layout again (see bug 2065 and 2878)
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.artdata = artdata;
opt.artifacttypes = artifacttypes;
opt.artifactcolors = artifactcolors;
opt.event = event;
opt.eventtypes = eventtypes;
opt.eventcolors = eventcolors;
opt.nanpaddata = []; % this is used to allow horizontal scaling to be constant (when looking at last segment continuous data, or when looking at segmented/zoomed-out non-continuous data)
opt.trllock = []; % this is used when zooming into trial based data
% save original layout when viewmode = component
if strcmp(cfg.viewmode, 'component')
opt.layouttopo = cfg.layout;
end
% open a new figure with the specified settings
h = open_figure(keepfields(cfg, {'figure', 'position', 'visible', 'renderer'}));
% check if the colormap is in proper format and set it
if ~isempty(cfg.colormap)
if ischar(cfg.colormap)
cfg.colormap = ft_colormap(cfg.colormap);
elseif iscell(cfg.colormap)
cfg.colormap = ft_colormap(cfg.colormap{:});
elseif isnumeric(cfg.colormap) && size(cfg.colormap,2)~=3
ft_error('cfg.colormap must be Nx3');
end
set(h, 'colormap', cfg.colormap);
end
% put appdata in figure
setappdata(h, 'opt', opt);
setappdata(h, 'cfg', cfg);
% set interruptible to off, see bug 3123
set(h, 'Interruptible', 'off', 'BusyAction', 'queue'); % enforce busyaction to queue to be sure
% enable custom data cursor text
dcm = datacursormode(h);
set(dcm, 'updatefcn', @cb_datacursortext);
% set the figure window title
funcname = mfilename();
if ~hasdata
if isfield(cfg, 'dataset')
dataname = cfg.dataset;
elseif isfield(cfg, 'datafile')
dataname = cfg.datafile;
else
dataname = [];
end
elseif isfield(cfg, 'inputfile') && ~isempty(cfg.inputfile)
dataname = cfg.inputfile;
else
dataname = inputname(2);
end
set(gcf, 'Name', sprintf('%d: %s: %s', double(gcf), funcname, join_str(', ',dataname)));
set(gcf, 'NumberTitle', 'off');
% set zoom option to on
% zoom(h, 'on')
% set(zoom(h), 'actionPostCallback', @zoom_drawlabels_cb)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% add the graphical user interface elements and callbacks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% these will be set using enable_callbacks after each redraw
set(h, 'KeyPressFcn', []);
set(h, 'WindowButtonDownFcn', []);
set(h, 'WindowButtonUpFcn', []);
set(h, 'WindowButtonMotionFcn', []);
if ~isempty(findobj(h, 'tag', 'navigateui'))
% assume that the graphical user interface elements are already present
% this speeds up plotting in cases like this https://www.fieldtriptoolbox.org/example/video_eeg/
else
% add the graphical user interface elements
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')
if strcmp(cfg.viewmode, 'component')
uicontrol('tag', 'labels', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', 'component', 'userdata', 'c')
else
uicontrol('tag', 'labels', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', 'channel', 'userdata', 'c')
end
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')
% preproc button - allows updating the preprocessing options on the fly
uicontrol('tag', 'rightcolumn', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', 'preproc', 'userdata', 'p', 'position', [0.91, 0.88, 0.08, 0.04])
% identify button - to find label of nearest channel to datapoint
uicontrol('tag', 'rightcolumn', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', 'identify', 'userdata', 'i', 'position', [0.91, 0.83, 0.08, 0.04])
% viewmode button to toggle between vertical and butterfly
if ~strcmp(cfg.viewmode, 'component')
uicontrol('tag', 'rightcolumn', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', 'viewmode', 'userdata', 'm', 'position', [0.91, 0.78, 0.08, 0.04])
end
% 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.850 - ((2+iArt-1)*0.09), 0.08, 0.04], 'backgroundcolor', opt.artifactcolors(iArt,:))
uicontrol('tag', 'artifactui', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', '<', 'userdata', ['shift+' num2str(iArt)], 'position', [0.91, 0.805 - ((2+iArt-1)*0.09), 0.03, 0.04], 'backgroundcolor', opt.artifactcolors(iArt,:))
uicontrol('tag', 'artifactui', 'parent', h, 'units', 'normalized', 'style', 'pushbutton', 'string', '>', 'userdata', ['control+' num2str(iArt)], 'position', [0.96, 0.805 - ((2+iArt-1)*0.09), 0.03, 0.04], 'backgroundcolor', opt.artifactcolors(iArt,:))
end
if length(artlabel)>1 % highlight the first one as active
arth = findobj(h, 'tag', 'artifactui');
arth = arth(end:-1:1); % order is reversed so reverse it again
hsel = [1 2 3] + (opt.artsel-1) .*3;
set(arth(hsel), 'fontweight', 'bold')
end
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', @keypress_cb);
ft_uilayout(h, 'tag', 'buttons', 'style', 'pushbutton', 'callback', @keypress_cb);
ft_uilayout(h, 'tag', 'rightcolumn', 'style', 'pushbutton', 'callback', @keypress_cb);
ft_uilayout(h, 'tag', 'artifactui', 'style', 'pushbutton', 'callback', @keypress_cb);
ft_uilayout(h, 'tag', 'labels', 'retag', 'navigateui'); % this renames the existing tags
ft_uilayout(h, 'tag', 'buttons', 'retag', 'navigateui'); % this renames the existing tags
ft_uilayout(h, 'tag', 'navigateui', 'BackgroundColor', [0.8 0.8 0.8], 'hpos', 'auto', 'vpos', 0);
% add a menu to the figure, but only if the current figure does not have subplots
tmpcfg = cfg;
if hasdata && isfield(data, 'cfg')
tmpcfg.previous = data.cfg;
end
menu_fieldtrip(h, tmpcfg, false);
end % if findobj navigateui
definetrial_cb(h);
redraw_cb(h);
help_cb(h);
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 = boolvec2artifact(opt.artdata.trial{1}(i,:));
end
% add the updated preproc to the output
try
browsecfg = getappdata(h, 'cfg');
cfg.preproc = browsecfg.preproc;
end
% add the updated events to the output cfg
cfg.event = opt.event;
end % if nargout
% do the general cleanup and bookkeeping at the end of the function
ft_postamble debug
ft_postamble previous data
ft_postamble provenance
if ~nargout
clear cfg
end
end % main function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function h = getparent(h)
p = h;
while p~=0
h = p;
p = get(h, 'parent');
end
end % function getparent
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function cursortext = cb_datacursortext(h, eventdata)
pos = get(eventdata, 'Position');
linetype = getappdata(eventdata.Target, 'datacursor_linetype');
if strcmp(linetype, 'event')
cursortext = sprintf('%s\nt = %g s', getappdata(eventdata.Target, 'datacursor_eventlabel'), getappdata(eventdata.Target, 'datacursor_eventtime'));
elseif strcmp(linetype, 'artifact')
cursortext = sprintf('%s\nt = %g s', getappdata(eventdata.Target, 'datacursor_artifactlabel'), getappdata(eventdata.Target, 'datacursor_artifacttime'));
elseif strcmp(linetype, 'channel')
% get plotted x axis
plottedX = get(eventdata.Target, 'xdata');
% determine values of data at real x axis
timeAxis = getappdata(eventdata.Target, 'datacursor_xdata');
dataAxis = getappdata(eventdata.Target, 'datacursor_ydata');
tInd = nearest(plottedX, pos(1));
% get label
chanLabel = getappdata(eventdata.Target, 'datacursor_label');
chanLabel = chanLabel{1};
cursortext = sprintf('%s = %g\nt = %g', chanLabel, dataAxis(tInd), timeAxis(tInd));
else
% explicitly tell the user there is no info because the x-axis and y-axis do not correspond to real data values (both are always between 0 and 1)
cursortext = '<no cursor available>';
end
end % function cb_datacursortext
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION see also lineattributes_common
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function color = colorcheck(color, n)
% define the mapping between color characters and RGB values
name = 'rgbcmywk';
rgb = [
1 0 0
0 1 0
0 0 1
0 1 1
1 0 1
1 1 0
1 1 1
0 0 0
];
% ensure that all colors are represented as RGB
if ischar(color)
original = color;
color = nan(length(original), 3);
for i=1:length(original)
color(i,:) = rgb(name==original(i),:);
end
end
% ensure that there are enough colors
color = repmat(color, ceil(n/size(color,1)), 1);
color = color(1:n,:);
end % function colorcheck
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function cleanup_cb(h, eventdata)
opt = getappdata(h, 'opt');
opt.cleanup = true;
setappdata(h, 'opt', opt);
uiresume
end % function cleanup_cb
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function definetrial_cb(h, eventdata)
h = getparent(h);
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');
%%%%%%%%%