clear
close all

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
global ft_default
ft_default = [];
ft_default.checksize = inf;
ft_default.reproducescript = 'reproduce';
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

datadir = '../rawdata';

%%

cfg = [];
cfg.dataset = fullfile(datadir, 'Subject01.ds');
cfg.trialfun = 'ft_trialfun_general';
cfg.trialdef.eventtype = 'backpanel trigger';
cfg.trialdef.eventvalue = 3;
cfg.trialdef.prestim = 1;
cfg.trialdef.poststim = 2;
cfg = ft_definetrial(cfg);
cfg.channel = {'MEG' 'EOG'};
cfg.continuous = 'yes';
dataFIC = ft_preprocessing(cfg);

%%

plot(dataFIC.time{1}, dataFIC.trial{1}(130,:))

%%

cfg = [];
cfg.dataset = fullfile(datadir, 'Subject01.ds');
cfg.trialdef.eventtype = 'backpanel trigger';
cfg.trialdef.eventvalue = 5;
cfg.trialdef.prestim = 1;
cfg.trialdef.poststim = 2;
cfg = ft_definetrial(cfg);
cfg.channel = {'MEG' 'EOG'};
cfg.continuous = 'yes';
dataIC = ft_preprocessing(cfg);

%%

cfg = [];
cfg.dataset = fullfile(datadir, 'Subject01.ds');
cfg.trialdef.eventtype = 'backpanel trigger';
cfg.trialdef.eventvalue = 9;
cfg.trialdef.prestim = 1;
cfg.trialdef.poststim = 2;
cfg = ft_definetrial(cfg);
cfg.channel = {'MEG' 'EOG'};
cfg.continuous = 'yes';
dataFC = ft_preprocessing(cfg);

%%

cfg = [];
avgFIC = ft_timelockanalysis(cfg, dataFIC);
avgFC = ft_timelockanalysis(cfg, dataFC);
avgIC = ft_timelockanalysis(cfg, dataIC);

%%

cfg = [];
cfg.showlabels = 'yes';
cfg.fontsize = 6;
cfg.layout = 'CTF151_helmet.mat';
cfg.ylim = [-3e-13 3e-13];
ft_multiplotER(cfg, avgFIC);

%%

cfg = [];
cfg.showlabels = 'no';
cfg.fontsize = 6;
cfg.layout = 'CTF151_helmet.mat';
cfg.baseline = [-0.2 0];
cfg.xlim = [-0.2 1.0];
cfg.ylim = [-3e-13 3e-13];
ft_multiplotER(cfg, avgFC, avgIC, avgFIC);

%%

cfg = [];
cfg.xlim = [-0.2 1.0];
cfg.ylim = [-1e-13 3e-13];
cfg.channel = 'MLC24';
ft_singleplotER(cfg,avgFC, avgIC, avgFIC);

%%

cfg = [];
cfg.xlim = [0.3 0.5];
cfg.colorbar = 'yes';
ft_topoplotER(cfg,avgFIC);

%%

cfg = [];
cfg.xlim = [-0.2 : 0.1 : 1.0];
cfg.zlim = [-2e-13 2e-13];
ft_topoplotER(cfg,avgFIC);

%%

cfg = [];
cfg.feedback = 'yes';
cfg.method = 'template';
cfg.neighbours = ft_prepare_neighbours(cfg, avgFIC);
cfg.planarmethod = 'sincos';
avgFICplanar = ft_megplanar(cfg, avgFIC);

%%

cfg = [];
avgFICplanarComb = ft_combineplanar(cfg, avgFICplanar);

%%

cfg = [];
cfg.xlim = [0.3 0.5];
cfg.zlim = 'maxmin';
cfg.colorbar = 'yes';
cfg.layout = 'CTF151_helmet.mat';
subplot(121);
ft_topoplotER(cfg,avgFIC)

cfg.zlim = 'maxabs';
cfg.layout = 'CTF151_helmet.mat';
subplot(122);
ft_topoplotER(cfg, avgFICplanarComb);

%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ft_default.reproducescript = []; % disable
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

