function plot_basic_func(subjnum)


%% 0.1 config

% ICA or not
ICA_switch    =1;

% How to load data
clear_data    =1;
load_org      =1;

% Operations
do_lowpass    =0;
do_baseline   =1;
do_planar     =0;
do_axial      =1;
do_gmf        =1;
do_tfr        =0;

% plot
plot_topoplot =0;
plot_gmf      =0;


% Subject name, etc.
subject_options;


% Load the Info
load([infopath,subjname{subjnum},'_info_maintask.mat']);
tempcfg=info.preproc.cfg;



%% 1 Load the data


%% 1.1 Load orignal data for TFR
if do_tfr||load_org
    cfg=info.preproc.cfg ;
    cfg.dataset =  [rawpath,cfg.dataset];
    data = ft_preprocessing(cfg);
    cfg=[];
    cfg.offset=-0.5*1200;
    data = ft_redefinetrial(cfg, data);
    
    
    if ICA_switch==1
        %% 1.1.1 load ICA results
        cfg = [];
        cfg.topo      = info.comp.topo;
        cfg.topolabel = info.comp.topolabel;
        comp_data     = ft_componentanalysis(cfg, data);
        %% 1.1.2 get clean data
        cfg = [];
        cfg.component=info.comp.artifact;
        data    = ft_rejectcomponent(cfg, comp_data,data);
    end
    
else
    %% 1.2 Load 200Hz version for ERF
    if ~exist('data_clean')||clear_data
        tic
        if ICA_switch==1
            load([matpath,subjname{subjnum},'_data_clean_ica.mat']);
        else
            load([matpath,subjname{subjnum},'_data_clean_noica.mat']);
        end
        
        toc
    end
end



%% 1.3 if need a low pass filter for ERF
cfg=[];
if do_lowpass
    cfg.lpfilter   = 'yes';
    cfg.lpfreq     =  30;
else
    cfg.lpfilter   = 'no';
    
end


%% 1.4 if baseline correction or not
if do_baseline
    cfg.demean     = 'yes';
    cfg.baselinewindow = [-0.1 0];
    [data] = ft_preprocessing(cfg, data);
end
data_clean=data;



%% 2. Processing the data

%% 2.1 preparation for Planar
if do_planar
    cfg=[];
    cfg.feedback='no';
    cfg.method='template';
    cfg.planarmethod='sincos';
    cfg.channel={'MEG'};
    cfg.trials='all';
    cfg.neighbours=ft_prepare_neighbours(cfg,data_clean);
    data_planar=ft_megplanar(cfg,data_clean);
end
cfg1=[];


%% 2.2 Define the code for expected and unexpected

% all_expect=[71,75,22,24];
% all_unexpect=[21,25,72,74];


all_expect=[25,72];
all_unexpect=[21,74];

% %%
% selected_C= trialinfo==21|trialinfo==74; % unexpect condition
% selected_D= trialinfo==72|trialinfo==25;
%
%     selected_C= trialinfo==71|trialinfo==24; % expect condition
%     selected_D= trialinfo==22|trialinfo==75;


% all_expect=[71,72];
% all_unexpect=[74,75];
do_expected_unexpected=1;

expected_code = all_expect;
unexpected_code = all_unexpect;
cfg=tempcfg;



%% 2.2.1 find the expected trials. find the same trial numbered expected trials as unexpected trials

if do_expected_unexpected
    select_trials = zeros(1,numel(cfg.trl(:,4)));
    for cc_i=1:numel(expected_code)
        select_trials = or(select_trials, locate_same_sametrialnum(cfg.trl(:,4),expected_code(cc_i),sum(locate_same(cfg.trl(:,4),unexpected_code(cc_i)))));
    end
    cfg.trials=select_trials';
else
    cfg.trials=(locate_same(cfg.trl(:,4),expected_code));
end

%% 2.3.1 axial analysis
if do_axial
    [timelock_exp]=ft_timelockanalysis(cfg,data_clean);
    cfg.baseline=[-0.2 0];
    avg_timelock_exp=ft_timelockbaseline(cfg,timelock_exp);
end

%% 2.4.1 planar analysis
if do_planar==1
    [timelock_exp_planar]=ft_timelockanalysis(cfg,data_planar);
    [avg_timelock_exp_p]=ft_combineplanar(cfg1,timelock_exp_planar);
    avg_timelock_exp_p.cfg=[];
end


%% 2.5.1 TFR analysis
if do_tfr
    cfg.method='wavelet';
    cfg.channel    = 'MEG';
    cfg.foi        = 1:2:100;
    cfg.toi        = -0.5:0.01:1.5;
    cfg.width      = linspace(1,14,numel(cfg.foi));
    cfg.output     = 'pow';
    cfg_tfr        = cfg;
    tfr_exp        = ft_freqanalysis(cfg_tfr,data_planar);
end



%% 2.2.2  find all the unexpected trials
cfg=tempcfg;
cfg.trials=(locate_same(cfg.trl(:,4),unexpected_code));


%% 2.3.2 axial analysis
if do_axial
    cfg.baseline=[-0.2 0];
    [timelock_unexp]=ft_timelockanalysis(cfg,data_clean);
    avg_timelock_unexp=ft_timelockbaseline(cfg,timelock_unexp);
end


%% 2.4.2 planar analysis
if do_planar==1
    [timelock_unexp_planar]=ft_timelockanalysis(cfg,data_planar);
    [avg_timelock_unexp_p]=ft_combineplanar(cfg1,timelock_unexp_planar);
    avg_timelock_unexp_p.cfg=[];
    savefast([matpath,'timelock_planar_',subjname{subjnum},'_200Hz_ica.mat'],'avg_timelock_exp_p','avg_timelock_unexp_p','select_trials');
end


%% 2.5.2 TFR analysis
if do_tfr
    cfg_tfr.trials=cfg.trials;
    tfr_unexp        =ft_freqanalysis(cfg_tfr,data_planar);
    savefast([matpath,'tfr_',num2str(subjnum),'.mat'],'tfr_exp','tfr_unexp');
end





%% 2.6 compute gloabal mean field
if do_gmf
    cfg.method       = 'amplitude';
    [gmf_exp]        = ft_globalmeanfield(cfg,avg_timelock_exp);
    [gmf_unexp]      = ft_globalmeanfield(cfg,avg_timelock_unexp);
    gmf_exp.time     = avg_timelock_exp.time;
    gmf_unexp.time   = avg_timelock_unexp.time;
    gmf_exp.cfg      = [];
    gmf_unexp.cfg    = [];
    savefast([matpath,'gmf_',subjname{subjnum},'_ICA_lp30_unexp.mat'],'gmf_exp','gmf_unexp');
    
end

end

%% 3. plot (can't use in the function)
%{
%% 3.1 GMF plot
print_figure=0;
if plot_gmf
    cfg.xlim    =  [-0.2:0.1:0.8];
    cfg.layout  =  'CTF275.lay';
    cfg.zlim    =  [-5e-14 5e-14];
    
    h=figure;
    subplot(5,5,subjnum);
    
    h=figure;
    plot(gmf_exp.time,mean(gmf_exp.avg,1),'b');title([subjname{subjnum}, ' GMF']);
    hold on
    plot(gmf_unexp.time,mean(gmf_unexp.avg,1),'r');%title('MEG grandaverage');
    legend('expected','unexpected');
    if print_figure
        h.PaperUnits='inch';
        h.PaperSize =[8,4];
        h.PaperPosition=[0,0,20,10];
        print(['sub',num2str(subjnum),'_ica.png'],'-dpng','-r400');
    end
end

%% 3.2 Topo plot
print_figure=0;
if plot_topoplot
    
    cfg.xlim    =  [-0.2:0.1:0.8];
    cfg.layout  =  'CTF275.lay';
    cfg.zlim    =  [-5e-14 5e-14];
    
    %             h=figure;
    %             %             subplot(2,2,1)
    %             ft_topoplotER(cfg,timelock1);title([subjname{subjnum},' axial (expected)']);
    %
    
    %             subplot(2,2,2)
    ft_topoplotER(cfg,timelock_exp_planar);title([subjname{subjnum},' planar (expected)']);
    h.PaperUnits='inch';
    h.PaperSize =[8,4];
    h.PaperPosition=[0,0,20,10];
    print(['sub',num2str(subjnum),'_exp.png'],'-dpng');
    
    
    %             h=figure;
    %
    %             ft_topoplotER(cfg,timelock2);title([subjname{subjnum},' axial (unexpected)']);
    %
    h=figure;
    ft_topoplotER(cfg,timelock_unexp_planar);title([subjname{subjnum},' planar (unexpected)']);
    h.PaperUnits='inch';
    h.PaperSize =[8,4];
    h.PaperPosition=[0,0,20,10];
    print(['sub',num2str(subjnum),'_unexp.png'],'-dpng');
end
end

% end

% savefast('avg_timelock_exp_unexp_noica_gmf.mat','avg_timelock_exp','avg_timelock_unexp');%,'avg_timelock_exp_p','avg_timelock_unexp_p');

%}