%% plot chargelitudes
clear all; close all;

feature = 'amplitude';
cultureID = {'xeko2','xeko3','xeko4'};
plot_colors = {'k','b','r'};

n_cutoff = 30;
figure(1); hold off;

xpos = 0;

for j = 1:length(cultureID)
    load([cultureID{j},'_alldata.mat']);
    switch feature
        case 'amplitude'
            f_ylabel = 'Amplitude (pA)';
            f_unit = 'pA';
            f_datcol = 3;
            f_bounds = [5 200];
        case 'frequency'
            f_ylabel = 'Frequency (Hz)';
            f_unit = 'Hz';
            f_datcol = 19;
            f_bounds = [1/20 1000];
        case 'decay'
            f_ylabel = 'Decay Time Constant (ms)';
            f_unit = 'ms';
            f_datcol = 5;
            f_bounds = [0.1 8];
        case 'charge'
            f_ylabel = 'Charge Transfer (fC)';
            f_unit = 'fC';
            f_datcol = 6;
            f_bounds = [1 inf];
    end
    overall = zeros(length(dat),3);
    xpos0 = xpos;
    for i = 1:length(dat)
        x = dat{i}(:,f_datcol);
        x = x(x>f_bounds(1) & x<f_bounds(2));
        subplot(2,1,1);
        xpos = xpos + 1;
        h1 = plot(xpos*ones(size(x))+0.15*randn(size(x)),x,'.');
        set(h1,'Color',plot_colors{j},'MarkerSize',2);
        tmp.mean(i) = nanmean(x);
        tmp.n(i) = length(x);
        tmp.stderr(i) = nanstd(x)/sqrt(tmp.n(i));
        disp([fidlist{i},': ',num2str(tmp.mean(i)),' +/- ', ...
            num2str(tmp.stderr(i)), ' ',f_unit, ...
            '   (n=',num2str(tmp.n(i)),')']);
        if tmp.n(i) >= n_cutoff
            goodCell = 1;
        else
            goodCell = nan; 
        end
        overall(i,:) = [goodCell tmp.mean(i) tmp.stderr(i)];
    end
    subplot(2,1,2); hold off;
    h2 = errorbar(xpos0+1:xpos,overall(xpos0:xpos,2),overall(xpos0:xpos,3),'o'); hold on;
    set(h2,'Color',plot_colors{j},'MarkerSize',3);
    goodCellMeans{j} = overall(:,1)*overall(:,2)';
end

for j = 1:length(cellID)
    for i = 1:2
        subplot(2,1,i);
        plot([0.5,xpos+0.5],[nanmean(goodCellMeans{j}) nanmean(goodCellMeans{j}),'--','Color',plot_colors{j}]);
        ylabel(f_ylabel,'FontSize',14);
        xlim([0 xpos+1]);
    end
end