forked from fieldtrip/fieldtrip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_databrowser.m
2205 lines (1970 loc) · 94 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 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.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.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 = 'yes')
% cfg.ploteventlabels = 'type=value', 'colorvalue' (default = 'type=value');
% cfg.plotevents = 'no' or 'yes', whether to plot event markers. (default is 'yes')
% cfg.viewmode = string, 'butterfly', 'vertical', 'component' for visualizing ICA/PCA components (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.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.renderer = string, 'opengl', 'zbuffer', 'painters', see MATLAB Figure Properties. If this function crashes, you should try 'painters'.
% cfg.position = location and size of the figure, specified as a vector of the form [left bottom width height].
%
% The following options for the scaling of 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.
% 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')
%
% 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
%
% 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')
% cfg.linewidth = number, width of plotted lines (default = 0.5)
%
% 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.
%
% 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-2015, 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$
% FIXME these should be removed or documented
% cfg.preproc
% cfg.channelcolormap
% cfg.colorgroups
% 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
ft_preamble trackconfig
% 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, '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, 'renamed', {'elecfile', 'elec'});
cfg = ft_checkconfig(cfg, 'renamed', {'gradfile', 'grad'});
cfg = ft_checkconfig(cfg, 'renamed', {'optofile', 'opto'});
% 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.artfctdef = ft_getopt(cfg, 'artfctdef', struct);
cfg.selectfeature = ft_getopt(cfg, 'selectfeature', 'visual'); % string or cell-array
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: '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.channelcolormap = ft_getopt(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]);
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.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.ploteventlabels = ft_getopt(cfg, 'ploteventlabels', 'type=value');
cfg.plotevents = ft_getopt(cfg, 'plotevents', 'yes');
cfg.precision = ft_getopt(cfg, 'precision', 'double');
cfg.zlim = ft_getopt(cfg, 'zlim', 'maxmin');
cfg.compscale = ft_getopt(cfg, 'compscale', 'global');
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.linewidth = ft_getopt(cfg, 'linewidth', 0.5);
cfg.verticalpadding = ft_getopt(cfg, 'verticalpadding', 'auto');
cfg.artifactalpha = ft_getopt(cfg, 'artifactalpha', 0.2); % for the opacity of marked artifacts
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');
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')
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
if size(cfg.chanscale,2) > size(cfg.chanscale,1)
cfg.chanscale = cfg.chanscale';
end
end
if ~isempty(cfg.mychanscale) && ~isfield(cfg, 'mychan')
ft_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 = keepfields(cfg, {'layout', 'rows', 'columns', 'commentpos', 'scalepos', 'elec', 'grad', 'opto', 'showcallinfo'});
if hasdata
cfg.layout = ft_prepare_layout(tmpcfg, data);
else
cfg.layout = ft_prepare_layout(tmpcfg);
end
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 isfield(data, 'cfg') && isfield(data.cfg, 'event')
% use the event structure from the data as per bug #2501
event = data.cfg.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);
event = [];
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, 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 = data.sampleinfo;
trlorg(:,3) = 0;
% 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
% construct trl-matrix for the data on disk
if ~isempty(cfg.trl)
% take the one specified by the user
trlorg = cfg.trl;
else
% make one that is according to the segments/trials in the data 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
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
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
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
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
% 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
% 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
% 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)
ft_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
ft_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)
ft_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
ft_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;
ft_info('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
ft_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 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}.chancolors = chancolors;
cfg.selfun{1} = 'simpleFFT';
% multiplotER
cfg.selcfg{2} = [];
cfg.selcfg{2}.layout = cfg.layout;
cfg.selfun{2} = 'multiplotER';
% topoplotER
cfg.selcfg{3} = [];
cfg.selcfg{3}.layout = cfg.layout;
cfg.selfun{3} = 'topoplotER';
% topoplotVAR
cfg.selcfg{4} = [];
cfg.selcfg{4}.layout = cfg.layout;
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}.anonimize = ft_getopt(cfg, 'anonimize');
cfg.selfun{6} = 'audiovideo';
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 read from 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.artifactcolors = [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 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.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
% set changedchanflg as true for initialization
opt.changedchanflg = true; % trigger for redrawing channel labels and preparing layout again (see bug 2065 and 2878)
% create fig
if isfield(cfg, 'position')
h = figure('Position', cfg.position);
else
h = figure;
end
% put appdata in figure
setappdata(h, 'opt', opt);
setappdata(h, 'cfg', cfg);
if ~isempty(cfg.renderer)
set(h, 'renderer', cfg.renderer);
end
% 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)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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'});
if any(strcmp(cfg.viewmode, {'component', 'vertical'}))
set(h, 'ReSizeFcn', @winresize_cb); % resize will now trigger redraw and replotting of labels
end
% 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')
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')
% 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.artifactcolors(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.artifactcolors(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.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.ftsel-1) .*3;
set(arth(hsel), 'fontweight', 'bold')
end
if true % strcmp(cfg.viewmode, 'butterfly')
% button to find label of nearest channel to datapoint
uicontrol('tag', 'buttons', '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;
end % if nargout
% do the general cleanup and bookkeeping at the end of the function
ft_postamble debug
ft_postamble trackconfig
ft_postamble previous data
ft_postamble provenance
ft_postamble hastoolbox
if ~nargout
clear cfg
end
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');