clearvars -except CNQX* ctrl*;
n = 27;
k = 0;

figure(1);

for i = 1:length(CNQX_sebastian)
    x = CNQX_sebastian{i}(CNQX_sebastian{i}>5);
    if length(x)>n
        k = k+1;
        xp = randperm(numel(x));
        y(:,k) = x(xp(1:n));
    end
end

for i = 1:length(ctrl_sebastian)
    x = ctrl_sebastian{i}(ctrl_sebastian{i}>5);
    if length(x)>n
        k = k+1;
        xp = randperm(numel(x));
        z(:,k) = x(xp(1:n));
    end
end

A_cnqx = reshape(y,[1,numel((y))]);
A_ctrl = reshape(z,[1,numel((z))]);

if max(A_cnqx) > max(A_ctrl)
    bins = 5:1:max(A_cnqx);
else
    bins = 5:1:max(A_ctrl);
end

N_ctrl = histc(A_ctrl,bins);
C_ctrl = cumsum(N_ctrl);
mu_ctrl = mean(A_ctrl);
sigma_ctrl = std(A_ctrl);
hold off;
plot(bins,C_ctrl/max(C_ctrl)*100,'-k','LineWidth',3);

N_cnqx = histc(A_cnqx,bins);
C_cnqx = cumsum(N_cnqx);
mu_cnqx = mean(A_cnqx);
sigma_cnqx = std(A_cnqx);
hold on;
plot(bins,C_cnqx/max(C_cnqx)*100,'-b','LineWidth',3);

axis([0 max(bins) 0 101])
set(gca,'FontSize',16);
legend('control','CNQX-treated','Location','SouthEast')
ylabel('Percentage of Events')
xlabel('Amplitude (pA)');
axis square;


mu_ctrl = mean(A_ctrl);
std_ctrl = std(A_ctrl);
mu_cnqx = mean(A_cnqx);
std_cnqx = std(A_cnqx);
% 
% figure(2);
% %errorbar([0 1]+randn*0.05,[mu_ctrl mu_cnqx],[std_ctrl std_cnqx]);
% errorbar([0 1]+randn*0.05,[mu_ctrl mu_cnqx],[std_ctrl std_cnqx]);
% hold all;