

function clusterplot = plot_clusterstats(rsawidth,rsastep,maxT,testType,nrep,rsa_tag,roi_tag,stats_tag,dir_tag)
    
    %load groupstats per cluster according to rsa settings and test
    fs = '\';
    if strcmp(dir_tag,'ERS')
        second_level_stats_dir = ['..\STATS' fs dir_tag fs rsa_tag fs stats_tag fs testType];   
        load([second_level_stats_dir fs roi_tag '_stats.mat'])
    elseif strcmp(dir_tag,'Btw_within')
        second_level_stats_dir = ['..\STATS' fs dir_tag fs rsa_tag fs testType];
        load([second_level_stats_dir fs roi_tag '_' stats_tag '_stats.mat'])
    elseif strcmp(dir_tag,'BtwWithin')
        second_level_stats_dir = ['..\STATS' fs dir_tag fs rsa_tag fs testType];
        load([second_level_stats_dir fs roi_tag '_' stats_tag '_stats.mat']);
    end

    if strcmp(testType,'memory') || strcmp(testType,'category') 
        stats = groupstats;
        data = stats.emp.t;
        axlim = [-3 3];
        axlab = 't-value';
        if strcmp(dir_tag,'BtwWithin')
            axlim = [-4 4];     
        end
        
    elseif strcmp(testType,'memoryXage') || strcmp(testType,'memoryXperformance')
        stats = stats;
        data = stats.emp.rho;
        axlim = [-0.8 0.8];
        axlab = 'spearman rho';
    end
    %prepare proper time tick labels according to window and RSA settings
    numWin = size(stats.emp.p,2);
    halfRSAwin = rsawidth / 2;
    time_ticks = [halfRSAwin:rsastep:maxT-2*rsastep];
    
    %set colormap
    cmap = flipud(cbrewer('div','RdBu',50)); cmap(cmap>1) = 1; cmap(cmap<0) = 0;
    
    %only empirical t-map with significant contour if available
    clusterplot = figure('Renderer', 'painters', 'Position', [50 50 1000 900]); 
    imagesc(squeeze(data)); set(gca,'Ydir','normal'); cb = colorbar; set(get(cb,'label'),'string',axlab,'fontsize',20); caxis(axlim); colormap(cmap);
    ylabel('encoding time (ms)','fontsize',25); xlabel('recognition time (ms)','fontsize',25); set(gca,'ytick',1:numWin,'yticklabel',time_ticks,'xtick',1:numWin,'xticklabel',time_ticks,'fontsize',20); xtickangle(90);
    sgtitle([testType ' | ' replace(roi_tag,'_',' ') ' | ' num2str(length(stats.subnum)) ' subs | ' num2str(sum(stats.channum)) ' chans | pos p = ' num2str(stats.emp_cluster.pos.p) ' | neg p = ' num2str(stats.emp_cluster.neg.p) ],'fontsize',20); hold on;

    
    %plot clusters found (solid for significant, dashed and grey for ns)
    %pos emp cluster
    if ~ strcmp(dir_tag,'Btw_within')
        mask = zeros(numWin,numWin);
    else
        mask = zeros(1,numWin); %if its Btw_within analysis (1-D!)
    end
    mask(stats.emp_cluster.pos.map == stats.emp_cluster.pos.idx) = 1;

    if ~isequal(mask,ones(numWin)) %if there is an empirical cluster
        cluster_edges = bwperimtrace(mask);
        for i = 1:1:length(cluster_edges)
            if stats.emp_cluster.pos.rank > nrep*0.95
                plot(cluster_edges{i}(:,1),cluster_edges{i}(:,2),'Color','black','LineWidth',3)
            else
                plot(cluster_edges{i}(:,1),cluster_edges{i}(:,2),'--','Color',[0.5 0.5 0.5],'LineWidth',3)
            end
            hold on
        end
    end

    %neg emp cluster
    if ~ strcmp(dir_tag,'Btw_within')
        mask = zeros(numWin,numWin);
    else
        mask = zeros(1,numWin); %if its Btw_within analysis (1-D!)
    end
    mask(stats.emp_cluster.neg.map == stats.emp_cluster.neg.idx) = 1;

    if ~isequal(mask,ones(numWin)) %if there is an empirical cluster
        cluster_edges = bwperimtrace(mask);
        for i = 1:1:length(cluster_edges)
            if stats.emp_cluster.neg.rank > nrep*0.95
                plot(cluster_edges{i}(:,1),cluster_edges{i}(:,2),'Color','black','LineWidth',3)
            else
                plot(cluster_edges{i}(:,1),cluster_edges{i}(:,2),'--','Color',[0.5 0.5 0.5],'LineWidth',3)
            end
            hold on
        end
    end

    if strcmp(dir_tag,'BtwWithin')
        if strcmp(stats_tag,'enc')
            ylabel('encoding time (ms)','fontsize',25); xlabel('encoding time (ms)','fontsize',25); set(gca,'ytick',1:numWin,'yticklabel',time_ticks,'xtick',1:numWin,'xticklabel',time_ticks,'fontsize',20); xtickangle(90);
            sgtitle([stats_tag ' | ' replace(roi_tag,'_',' ') ' | ' num2str(length(stats.subnum)) ' subs | ' num2str(sum(stats.channum)) ' chans | pos p = ' num2str(stats.emp_cluster.pos.p) ' | neg p = ' num2str(stats.emp_cluster.neg.p) ],'fontsize',20); hold on;
        elseif strcmp(stats_tag,'rec')
            ylabel('recognition time (ms)','fontsize',25); xlabel('recognition time (ms)','fontsize',25); set(gca,'ytick',1:numWin,'yticklabel',time_ticks,'xtick',1:numWin,'xticklabel',time_ticks,'fontsize',20); xtickangle(90);
            sgtitle([stats_tag ' | ' replace(roi_tag,'_',' ') ' | ' num2str(length(stats.subnum)) ' subs | ' num2str(sum(stats.channum)) ' chans | pos p = ' num2str(stats.emp_cluster.pos.p) ' | neg p = ' num2str(stats.emp_cluster.neg.p) ],'fontsize',20); hold on;
        end
    end
    
    %save
    %saveas(gcf,[[second_level_stats_dir fs roi_tag '_cluster.svg'])
    if strcmp(dir_tag,'BtwERS') || strcmp(dir_tag,'Btw_within') || strcmp(dir_tag,'BtwWithin')
        saveas(gcf,[second_level_stats_dir fs roi_tag '_' stats_tag '_cluster.png'])
    else
        saveas(gcf,[second_level_stats_dir fs roi_tag '_cluster.png'])
    end
end