close all;
feature.name = 'frequency';
n_cutoff = 38;

% -- setting values for feature of interest -- %
switch feature.name
    case 'amplitude'
        feature.label = 'Amplitude';
        feature.unit = 'pA';
        feature.datcol = 3;
        feature.bounds = [5 400];
        feature.cumhist = 1;
    case 'frequency'
        feature.label = 'Frequency';
        feature.unit = 'Hz';
        feature.datcol = 19;
        feature.bounds = [1/20 1000];
        feature.cumhist = 0;
    case 'decay'
        feature.label = 'Decay Time Constant';
        feature.unit = 'ms';
        feature.datcol = 5;
        feature.bounds = [0.2 10];
        feature.cumhist = 0;
    case 'charge'
        feature.label = 'Charge Transfer';
        feature.unit = 'fC';
        feature.datcol = 6;
        feature.bounds = [1 inf];
        feature.cumhist = 0;
end

%% Generate Cell Averages

% % ctrl
% for i = 1:length(minis.ctrl)
%     cellavg.ctrl.mean(i,1) = nanmean(minis.ctrl{i,1}(~isinf(minis.ctrl{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.ctrl.n(i,1) = length(minis.ctrl{i,1}(~isinf(minis.ctrl{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.ctrl.std(i,1) = nanstd(minis.ctrl{i,1}(~isinf(minis.ctrl{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.ctrl.feature = feature;
% end
% ctrlCellAvg = cellavg.ctrl.mean(cellavg.ctrl.n>=n_cutoff);

% % TTX
% for i = 1:length(minis.TTX)
%     cellavg.TTX.mean(i,1) = nanmean(minis.TTX{i,1}(~isinf(minis.TTX{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.TTX.n(i,1) = length(minis.TTX{i,1}(~isinf(minis.TTX{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.TTX.std(i,1) = nanstd(minis.TTX{i,1}(~isinf(minis.TTX{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.TTX.feature = feature;
% end
% TTXCellAvg = cellavg.TTX.mean(cellavg.TTX.n>=n_cutoff);

% % TTX+CTZ
% for i = 1:length(minis.TTXCTZ)
%     cellavg.TTXCTZ.mean(i,1) = nanmean(minis.TTXCTZ{i,1}(~isinf(minis.TTXCTZ{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.TTXCTZ.n(i,1) = length(minis.TTXCTZ{i,1}(~isinf(minis.TTXCTZ{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.TTXCTZ.std(i,1) = nanstd(minis.TTXCTZ{i,1}(~isinf(minis.TTXCTZ{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.TTXCTZ.feature = feature;
% end
% TTXCTZCellAvg = cellavg.TTXCTZ.mean(cellavg.TTXCTZ.n>=n_cutoff);

% % CNQX
% for i = 1:length(minis.CNQX)
%     cellavg.CNQX.mean(i,1) = nanmean(minis.CNQX{i,1}(~isinf(minis.CNQX{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.CNQX.n(i,1) = length(minis.CNQX{i,1}(~isinf(minis.CNQX{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.CNQX.std(i,1) = nanstd(minis.CNQX{i,1}(~isinf(minis.CNQX{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.CNQX.feature = feature;
% end
% CNQXCellAvg = cellavg.CNQX.mean(cellavg.CNQX.n>=n_cutoff);

% % CNQX+stim1X
% for i = 1:length(minis.CNQXstim1X)
%     cellavg.CNQXstim1X.mean(i,1) = nanmean(minis.CNQXstim1X{i,1}(~isinf(minis.CNQXstim1X{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.CNQXstim1X.n(i,1) = length(minis.CNQXstim1X{i,1}(~isinf(minis.CNQXstim1X{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.CNQXstim1X.std(i,1) = nanstd(minis.CNQXstim1X{i,1}(~isinf(minis.CNQXstim1X{i,1}(:,feature.datcol)),feature.datcol));
%     cellavg.CNQXstim1X.feature = feature;
% end
% CNQXstim1XCellAvg = cellavg.CNQXstim1X.mean(cellavg.CNQXstim1X.n>=n_cutoff);

% preCTZ
for i = 1:length(minis.preCTZ)
    cellavg.preCTZ.mean(i,1) = nanmean(minis.preCTZ{i,1}(~isinf(minis.preCTZ{i,1}(:,feature.datcol)),feature.datcol));
    cellavg.preCTZ.n(i,1) = length(minis.preCTZ{i,1}(~isinf(minis.preCTZ{i,1}(:,feature.datcol)),feature.datcol));
    cellavg.preCTZ.std(i,1) = nanstd(minis.preCTZ{i,1}(~isinf(minis.preCTZ{i,1}(:,feature.datcol)),feature.datcol));
    cellavg.preCTZ.feature = feature;
end
preCTZCellAvg = cellavg.preCTZ.mean(cellavg.preCTZ.n>=n_cutoff);

% duringCTZ
for i = 1:length(minis.duringCTZ)
    cellavg.duringCTZ.mean(i,1) = nanmean(minis.duringCTZ{i,1}(~isinf(minis.duringCTZ{i,1}(:,feature.datcol)),feature.datcol));
    cellavg.duringCTZ.n(i,1) = length(minis.duringCTZ{i,1}(~isinf(minis.duringCTZ{i,1}(:,feature.datcol)),feature.datcol));
    cellavg.duringCTZ.std(i,1) = nanstd(minis.duringCTZ{i,1}(~isinf(minis.duringCTZ{i,1}(:,feature.datcol)),feature.datcol));
    cellavg.duringCTZ.feature = feature;
end
duringCTZCellAvg = cellavg.duringCTZ.mean(cellavg.duringCTZ.n>=n_cutoff);

%% Generate Cumulative Histogram Sample

dI = 1;
bin = 4:1:150;

% sampdata_ch.ctrl = [];
% for i_dat = 1:length(minis.ctrl)
%     if length(minis.ctrl{i_dat,1}) >= n_cutoff
%         sampdata_ch.ctrl = [sampdata_ch.ctrl; randsample(minis.ctrl{i_dat,1}(:,feature.datcol),n_cutoff)];
%     end
% end
% cumhist.ctrl = cumsum(histc(sampdata_ch.ctrl,bin));

% sampdata_ch.TTX = [];
% for i_dat = 1:length(minis.TTX)
%     if length(minis.TTX{i_dat,1}) >= n_cutoff
%         sampdata_ch.TTX = [sampdata_ch.TTX; randsample(minis.TTX{i_dat,1}(:,feature.datcol),n_cutoff)];
%     end
% end
% cumhist.TTX = cumsum(histc(sampdata_ch.TTX,bin));

% sampdata_ch.TTXCTZ = [];
% for i_dat = 1:length(minis.TTXCTZ)
%     if length(minis.TTXCTZ{i_dat,1}) >= n_cutoff
%         sampdata_ch.TTXCTZ = [sampdata_ch.TTXCTZ; randsample(minis.TTXCTZ{i_dat,1}(:,feature.datcol),n_cutoff)];
%     end
% end
% cumhist.TTXCTZ = cumsum(histc(sampdata_ch.TTXCTZ,bin));

% sampdata_ch.CNQX = [];
% for i_dat = 1:length(minis.CNQX)
%     if length(minis.CNQX{i_dat,1}) >= n_cutoff
%         sampdata_ch.CNQX = [sampdata_ch.CNQX; randsample(minis.CNQX{i_dat,1}(:,feature.datcol),n_cutoff)];
%     end
% end
% cumhist.CNQX = cumsum(histc(sampdata_ch.CNQX,bin));

% sampdata_ch.CNQXstim1X = [];
% for i_dat = 1:length(minis.CNQXstim1X)
%     if length(minis.CNQXstim1X{i_dat,1}) >= n_cutoff
%         sampdata_ch.CNQXstim1X = [sampdata_ch.CNQXstim1X; randsample(minis.CNQXstim1X{i_dat,1}(:,feature.datcol),n_cutoff)];
%     end
% end
% cumhist.CNQXstim1X = cumsum(histc(sampdata_ch.CNQXstim1X,bin));

sampdata_ch.preCTZ = [];
for i_dat = 1:length(minis.preCTZ)
    if length(minis.preCTZ{i_dat,1}) >= n_cutoff
        sampdata_ch.preCTZ = [sampdata_ch.preCTZ; randsample(minis.preCTZ{i_dat,1}(:,feature.datcol),n_cutoff)];
    end
end
cumhist.preCTZ = cumsum(histc(sampdata_ch.preCTZ,bin));

sampdata_ch.duringCTZ = [];
for i_dat = 1:length(minis.duringCTZ)
    if length(minis.duringCTZ{i_dat,1}) >= n_cutoff
        sampdata_ch.duringCTZ = [sampdata_ch.duringCTZ; randsample(minis.duringCTZ{i_dat,1}(:,feature.datcol),n_cutoff)];
    end
end
cumhist.duringCTZ = cumsum(histc(sampdata_ch.duringCTZ,bin));

%% Generate Rank Order Sample

% % TTX vs ctrl
% ro_maxsample = min([length(sampdata_ch.ctrl),length(sampdata_ch.TTX)]);
% sampdata_ro.ctrl = sortrows(randsample(sampdata_ch.ctrl,ro_maxsample));
% sampdata_ro.TTX = sortrows(randsample(sampdata_ch.TTX,ro_maxsample));

% % CNQX vs ctrl
% ro_maxsample = min([length(sampdata_ch.ctrl),length(sampdata_ch.CNQX)]);
% sampdata_ro.ctrl = sortrows(randsample(sampdata_ch.ctrl,ro_maxsample));
% sampdata_ro.CNQX = sortrows(randsample(sampdata_ch.CNQX,ro_maxsample));

% % CNQX vs CNQXstim1X ctrl
% ro_maxsample = min([length(sampdata_ch.ctrl),length(sampdata_ch.CNQX),length(sampdata_ch.CNQXstim1X)]);
% sampdata_ro.ctrl = sortrows(randsample(sampdata_ch.ctrl,ro_maxsample));
% sampdata_ro.CNQX = sortrows(randsample(sampdata_ch.CNQX,ro_maxsample));
% sampdata_ro.CNQXstim1X = sortrows(randsample(sampdata_ch.CNQXstim1X,ro_maxsample));

% % TTX vs TTX+CTZ ctrl
% ro_maxsample = min([length(sampdata_ch.ctrl),length(sampdata_ch.TTX),length(sampdata_ch.TTXCTZ)]);
% sampdata_ro.ctrl = sortrows(randsample(sampdata_ch.ctrl,ro_maxsample));
% sampdata_ro.TTX = sortrows(randsample(sampdata_ch.TTX,ro_maxsample));
% sampdata_ro.TTXCTZ = sortrows(randsample(sampdata_ch.TTXCTZ,ro_maxsample));

% duringCTZ vs preCTZ
ro_maxsample = min([length(sampdata_ch.preCTZ),length(sampdata_ch.duringCTZ)]);
sampdata_ro.preCTZ = sortrows(randsample(sampdata_ch.preCTZ,ro_maxsample));
sampdata_ro.duringCTZ = sortrows(randsample(sampdata_ch.duringCTZ,ro_maxsample));

%% Plot Cell Means & StdErr
close all;
figure(1);

% % TTX vs Ctrl
% bar([1 2],[nanmean(ctrlCellAvg) nanmean(TTXCellAvg)],'FaceColor','k','LineWidth',1);
% hold on;
% errorbar(1,nanmean(ctrlCellAvg),nanstd(ctrlCellAvg)/sqrt(length(ctrlCellAvg)),'k','LineWidth',2);
% errorbar(2,nanmean(TTXCellAvg),nanstd(TTXCellAvg)/sqrt(length(TTXCellAvg)),'k','LineWidth',2);
% text(1,0,num2str(length(ctrlCellAvg)),'VerticalAlignment','bottom','HorizontalAlignment','center','Color','w');
% text(2,0,num2str(length(TTXCellAvg)),'VerticalAlignment','bottom','HorizontalAlignment','center','Color','w');
% set(gca,'XTick',[1,2],'XTickLabel',{'control','TTX'},'FontSize',14);

% bar([1 2 3],[nanmean(ctrlCellAvg) nanmean(TTXCellAvg) nanmean(TTXCTZCellAvg)],'FaceColor','k','LineWidth',1);
% hold on;
% errorbar(1,nanmean(ctrlCellAvg),nanstd(ctrlCellAvg)/sqrt(length(ctrlCellAvg)),'k','LineWidth',2);
% errorbar(2,nanmean(TTXCellAvg),nanstd(TTXCellAvg)/sqrt(length(TTXCellAvg)),'k','LineWidth',2);
% errorbar(3,nanmean(TTXCTZCellAvg),nanstd(TTXCTZCellAvg)/sqrt(length(TTXCTZCellAvg)),'k','LineWidth',2);
% set(gca,'XTick',[1,2,3],'XTickLabel',{'control','TTX','TTX+CTZ'},'FontSize',14);

% % CNQX vs Ctrl
% bar([1 2],[nanmean(ctrlCellAvg) nanmean(CNQXCellAvg)],'FaceColor','k','LineWidth',1);
% hold on;
% errorbar(1,nanmean(ctrlCellAvg),nanstd(ctrlCellAvg)/sqrt(length(ctrlCellAvg)),'k','LineWidth',2);
% errorbar(2,nanmean(CNQXCellAvg),nanstd(CNQXCellAvg)/sqrt(length(CNQXCellAvg)),'k','LineWidth',2);
% text(1,0,num2str(length(ctrlCellAvg)),'VerticalAlignment','bottom','HorizontalAlignment','center','Color','w');
% text(2,0,num2str(length(CNQXCellAvg)),'VerticalAlignment','bottom','HorizontalAlignment','center','Color','w');
% set(gca,'XTick',[1,2],'XTickLabel',{'control','CNQX'},'FontSize',14);

% % CNQX vs. CNQX+stim1X vs. Ctrl
% bar([1 2 3],[nanmean(ctrlCellAvg) nanmean(CNQXCellAvg) nanmean(CNQXstim1XCellAvg)],'FaceColor','k','LineWidth',1);
% hold on;
% errorbar(1,nanmean(ctrlCellAvg),nanstd(ctrlCellAvg)/sqrt(length(ctrlCellAvg)),'k','LineWidth',2);
% errorbar(2,nanmean(CNQXCellAvg),nanstd(CNQXCellAvg)/sqrt(length(CNQXCellAvg)),'k','LineWidth',2);
% errorbar(3,nanmean(CNQXstim1XCellAvg),nanstd(CNQXstim1XCellAvg)/sqrt(length(CNQXstim1XCellAvg)),'k','LineWidth',2);
% text(1,0,num2str(length(ctrlCellAvg)),'VerticalAlignment','bottom','HorizontalAlignment','center','Color','w');
% text(2,0,num2str(length(CNQXCellAvg)),'VerticalAlignment','bottom','HorizontalAlignment','center','Color','w');
% text(3,0,num2str(length(CNQXstim1XCellAvg)),'VerticalAlignment','bottom','HorizontalAlignment','center','Color','w');
% set(gca,'XTick',[1,2,3],'XTickLabel',{'control','CNQX','CNQX+stim'},'FontSize',14);

% % TTX vs. TTX+CTZ vs. Ctrl
% bar([1 2 3],[nanmean(ctrlCellAvg) nanmean(TTXCellAvg) nanmean(TTXCTZCellAvg)],'FaceColor','k','LineWidth',1);
% hold on;
% errorbar(1,nanmean(ctrlCellAvg),nanstd(ctrlCellAvg)/sqrt(length(ctrlCellAvg)),'k','LineWidth',2);
% errorbar(2,nanmean(TTXCellAvg),nanstd(TTXCellAvg)/sqrt(length(TTXCellAvg)),'k','LineWidth',2);
% errorbar(3,nanmean(TTXCTZCellAvg),nanstd(TTXCTZCellAvg)/sqrt(length(TTXCTZCellAvg)),'k','LineWidth',2);
% text(1,0,num2str(length(ctrlCellAvg)),'VerticalAlignment','bottom','HorizontalAlignment','center','Color','w');
% text(2,0,num2str(length(TTXCellAvg)),'VerticalAlignment','bottom','HorizontalAlignment','center','Color','w');
% text(3,0,num2str(length(TTXCTZCellAvg)),'VerticalAlignment','bottom','HorizontalAlignment','center','Color','w');
% set(gca,'XTick',[1,2,3],'XTickLabel',{'control','TTX','TTX+CTZ'},'FontSize',14);

% duringCTZ vs preCTZ
bar([1 2],[nanmean(preCTZCellAvg) nanmean(duringCTZCellAvg)],'FaceColor','k','LineWidth',1);
hold on;
errorbar(1,nanmean(preCTZCellAvg),nanstd(preCTZCellAvg)/sqrt(length(preCTZCellAvg)),'k','LineWidth',2);
errorbar(2,nanmean(duringCTZCellAvg),nanstd(duringCTZCellAvg)/sqrt(length(duringCTZCellAvg)),'k','LineWidth',2);
text(1,0,num2str(length(preCTZCellAvg)),'VerticalAlignment','bottom','HorizontalAlignment','center','Color','w');
text(2,0,num2str(length(duringCTZCellAvg)),'VerticalAlignment','bottom','HorizontalAlignment','center','Color','w');
set(gca,'XTick',[1,2],'XTickLabel',{'control','duringCTZ'},'FontSize',14);

set(gca,'LineWidth',0.5);
xlim([0.25 3.75]);
box off;
axis square;
set(gcf,'Position',[440 378 560 420]);

%% Plot Rank Order

figure(2);
hold off;

% % TTX vs. Ctrl
% plot(sampdata_ro.ctrl,sampdata_ro.TTX,'ok');
% hold on;
% ax_lim = 100;
% s.TTXvsCtrl = regstats(sampdata_ro.TTX,sampdata_ro.ctrl,'linear',{'rsquare','beta'});
% text(ax_lim/2,0,['R^{2} = ',num2str(s.TTXvsCtrl.rsquare)],'HorizontalAlignment','center','VerticalAlignment','bottom')
% plot([0 ax_lim],[0 ax_lim]*s.TTXvsCtrl.beta(2)+s.TTXvsCtrl.beta(1),'-m');
% plot(sampdata_ro.ctrl,(sampdata_ro.TTX-s.TTXvsCtrl.beta(1))/s.TTXvsCtrl.beta(2),'om');
% plot([0 ax_lim],[0 ax_lim],'-k');

% % CNQX vs. Ctrl
% plot(sampdata_ro.ctrl,sampdata_ro.CNQX,'ok');
% hold on;
% ax_lim = 100;
% s.CNQXvsCtrl = regstats(sampdata_ro.CNQX,sampdata_ro.ctrl,'linear',{'rsquare','beta'});
% text(ax_lim/2,0,['R^{2} = ',num2str(s.CNQXvsCtrl.rsquare)],'HorizontalAlignment','center','VerticalAlignment','bottom')
% plot([0 ax_lim],[0 ax_lim]*s.CNQXvsCtrl.beta(2)+s.CNQXvsCtrl.beta(1),'-r');
% plot(sampdata_ro.ctrl,(sampdata_ro.CNQX-s.CNQXvsCtrl.beta(1))/s.CNQXvsCtrl.beta(2),'or');
% plot([0 ax_lim],[0 ax_lim],'-k');

% % CNQX vs. CNQX+stim vs. Ctrl
% ax_lim = 100;
% %--cnqx vs ctrl
% plot(sampdata_ro.ctrl,sampdata_ro.CNQX,'ok');
% hold on;
% s.CNQXvsCtrl = regstats(sampdata_ro.CNQX,sampdata_ro.ctrl,'linear',{'rsquare','beta'});
% text(ax_lim/2,0,['R^{2} = ',num2str(s.CNQXvsCtrl.rsquare)],'HorizontalAlignment','center','VerticalAlignment','bottom')
% plot([0 ax_lim],[0 ax_lim]*s.CNQXvsCtrl.beta(2)+s.CNQXvsCtrl.beta(1),'-r');
% plot(sampdata_ro.ctrl,(sampdata_ro.CNQX-s.CNQXvsCtrl.beta(1))/s.CNQXvsCtrl.beta(2),'or');
% plot([0 ax_lim],[0 ax_lim],'-k');
% %--cnqx+stim vs ctrl
% figure(3); hold off;
% plot(sampdata_ro.ctrl,sampdata_ro.CNQXstim1X,'ok');
% hold on;
% s.CNQXstim1XvsCtrl = regstats(sampdata_ro.CNQXstim1X,sampdata_ro.ctrl,'linear',{'rsquare','beta'});
% text(ax_lim/2,0,['R^{2} = ',num2str(s.CNQXstim1XvsCtrl.rsquare)],'HorizontalAlignment','center','VerticalAlignment','bottom')
% plot([0 ax_lim],[0 ax_lim]*s.CNQXstim1XvsCtrl.beta(2)+s.CNQXstim1XvsCtrl.beta(1),'-b');
% plot(sampdata_ro.ctrl,(sampdata_ro.CNQXstim1X-s.CNQXstim1XvsCtrl.beta(1))/s.CNQXstim1XvsCtrl.beta(2),'ob');
% plot([0 ax_lim],[0 ax_lim],'-k');
% %--cnqx+stim vs. cnqx
% figure(4); hold off;
% plot(sampdata_ro.CNQX,sampdata_ro.CNQXstim1X,'ob');
% hold on;
% s.CNQXstim1XvsCNQX = regstats(sampdata_ro.CNQXstim1X,sampdata_ro.CNQX,'linear',{'rsquare','beta'});
% text(ax_lim/2,0,['R^{2} = ',num2str(s.CNQXstim1XvsCtrl.rsquare)],'HorizontalAlignment','center','VerticalAlignment','bottom')
% plot([0 ax_lim],[0 ax_lim]*s.CNQXstim1XvsCNQX.beta(2)+s.CNQXstim1XvsCNQX.beta(1),'-r');
% plot(sampdata_ro.CNQX,(sampdata_ro.CNQXstim1X-s.CNQXstim1XvsCNQX.beta(1))/s.CNQXstim1XvsCNQX.beta(2),'or');
% plot([0 ax_lim],[0 ax_lim],'-b');

% % TTX vs. TTX+CTZ vs. Ctrl
% ax_lim = 100;
% %--ttx vs ctrl
% plot(sampdata_ro.ctrl,sampdata_ro.TTX,'ok');
% hold on;
% s.TTXvsCtrl = regstats(sampdata_ro.TTX,sampdata_ro.ctrl,'linear',{'rsquare','beta'});
% text(ax_lim/2,0,['R^{2} = ',num2str(s.TTXvsCtrl.rsquare)],'HorizontalAlignment','center','VerticalAlignment','bottom')
% plot([0 ax_lim],[0 ax_lim]*s.TTXvsCtrl.beta(2)+s.TTXvsCtrl.beta(1),'-m');
% plot(sampdata_ro.ctrl,(sampdata_ro.TTX-s.TTXvsCtrl.beta(1))/s.TTXvsCtrl.beta(2),'om');
% plot([0 ax_lim],[0 ax_lim],'-k');
% %--ttx+ctz vs ctrl
% figure(3); hold off;
% plot(sampdata_ro.ctrl,sampdata_ro.TTXCTZ,'ok');
% hold on;
% s.TTXCTZvsCtrl = regstats(sampdata_ro.TTXCTZ,sampdata_ro.ctrl,'linear',{'rsquare','beta'});
% text(ax_lim/2,0,['R^{2} = ',num2str(s.TTXCTZvsCtrl.rsquare)],'HorizontalAlignment','center','VerticalAlignment','bottom')
% plot([0 ax_lim],[0 ax_lim]*s.TTXCTZvsCtrl.beta(2)+s.TTXCTZvsCtrl.beta(1),'-g');
% plot(sampdata_ro.ctrl,(sampdata_ro.TTXCTZ-s.TTXCTZvsCtrl.beta(1))/s.TTXCTZvsCtrl.beta(2),'og');
% plot([0 ax_lim],[0 ax_lim],'-k');
% %--ttx+ctz vs. ttx
% figure(4); hold off;
% plot(sampdata_ro.TTX,sampdata_ro.TTXCTZ,'og');
% hold on;
% s.TTXCTZvsTTX = regstats(sampdata_ro.TTXCTZ,sampdata_ro.TTX,'linear',{'rsquare','beta'});
% text(ax_lim/2,0,['R^{2} = ',num2str(s.TTXCTZvsCtrl.rsquare)],'HorizontalAlignment','center','VerticalAlignment','bottom')
% plot([0 ax_lim],[0 ax_lim]*s.TTXCTZvsTTX.beta(2)+s.TTXCTZvsTTX.beta(1),'-m');
% plot(sampdata_ro.TTX,(sampdata_ro.TTXCTZ-s.TTXCTZvsTTX.beta(1))/s.TTXCTZvsTTX.beta(2),'om');
% plot([0 ax_lim],[0 ax_lim],'-g');

% duringCTZ vs. preCTZ
plot(sampdata_ro.preCTZ,sampdata_ro.duringCTZ,'ok');
hold on;
ax_lim = 100;
s.duringCTZvspreCTZ = regstats(sampdata_ro.duringCTZ,sampdata_ro.preCTZ,'linear',{'rsquare','beta'});
text(ax_lim/2,0,['R^{2} = ',num2str(s.duringCTZvspreCTZ.rsquare)],'HorizontalAlignment','center','VerticalAlignment','bottom')
plot([0 ax_lim],[0 ax_lim]*s.duringCTZvspreCTZ.beta(2)+s.duringCTZvspreCTZ.beta(1),'-r');
plot(sampdata_ro.preCTZ,(sampdata_ro.duringCTZ-s.duringCTZvspreCTZ.beta(1))/s.duringCTZvspreCTZ.beta(2),'or');
plot([0 ax_lim],[0 ax_lim],'-k');


for i = 2:4
    figure(i)
set(gca,'LineWidth',0.5);
axis([0 ax_lim 0 ax_lim]);
box off;
axis square;
set(gcf,'Position',[440 378 560 420]);
end
%% Plot Cumulative Histogram

figure(5);
hold off;

% % TTX vs Ctrl
% plot(bin,cumhist.ctrl/max(cumhist.ctrl),'-k','LineWidth',2);
% hold on;
% plot(bin,cumhist.TTX/max(cumhist.TTX),'-m','LineWidth',2);
% sampdata_ch.TTXscaled = (sampdata_ch.TTX-s.TTXvsCtrl.beta(1))/s.TTXvsCtrl.beta(2);
% cumhist.TTXscaled = cumsum(histc(sampdata_ch.TTXscaled,bin));
% plot(bin,cumhist.TTXscaled/max(cumhist.TTXscaled),'--m','LineWidth',2);

% % CNQX vs Ctrl
% plot(bin,cumhist.ctrl/max(cumhist.ctrl),'-k','LineWidth',2);
% hold on;
% plot(bin,cumhist.CNQX/max(cumhist.CNQX),'-r','LineWidth',2);
% sampdata_ch.CNQXscaled = (sampdata_ch.CNQX-s.CNQXvsCtrl.beta(1))/s.CNQXvsCtrl.beta(2);
% cumhist.CNQXscaled = cumsum(histc(sampdata_ch.CNQXscaled,bin));
% plot(bin,cumhist.CNQXscaled/max(cumhist.CNQXscaled),'--r','LineWidth',2);

% % CNQX vs CNQXstim1X vs. Ctrl
% plot(bin,cumhist.ctrl/max(cumhist.ctrl),'-k','LineWidth',2);
% hold on;
% plot(bin,cumhist.CNQX/max(cumhist.CNQX),'-r','LineWidth',2);
% plot(bin,cumhist.CNQXstim1X/max(cumhist.CNQXstim1X),'-b','LineWidth',2);
% sampdata_ch.CNQXscaled = (sampdata_ch.CNQX-s.CNQXvsCtrl.beta(1))/s.CNQXvsCtrl.beta(2);
% sampdata_ch.CNQXstim1Xscaled = (sampdata_ch.CNQXstim1X-s.CNQXstim1XvsCtrl.beta(1))/s.CNQXstim1XvsCtrl.beta(2);
% cumhist.CNQXscaled = cumsum(histc(sampdata_ch.CNQXscaled,bin));
% cumhist.CNQXstim1Xscaled = cumsum(histc(sampdata_ch.CNQXstim1Xscaled,bin));
% plot(bin,cumhist.CNQXscaled/max(cumhist.CNQXscaled),'--r','LineWidth',2);
% plot(bin,cumhist.CNQXstim1Xscaled/max(cumhist.CNQXstim1Xscaled),'--b','LineWidth',2);

% % TTX vs TTXCTZ vs. Ctrl
% plot(bin,cumhist.ctrl/max(cumhist.ctrl),'-k','LineWidth',2);
% hold on;
% plot(bin,cumhist.TTX/max(cumhist.TTX),'-m','LineWidth',2);
% plot(bin,cumhist.TTXCTZ/max(cumhist.TTXCTZ),'-g','LineWidth',2);
% sampdata_ch.TTXscaled = (sampdata_ch.TTX-s.TTXvsCtrl.beta(1))/s.TTXvsCtrl.beta(2);
% sampdata_ch.TTXCTZscaled = (sampdata_ch.TTXCTZ-s.TTXCTZvsCtrl.beta(1))/s.TTXCTZvsCtrl.beta(2);
% cumhist.TTXscaled = cumsum(histc(sampdata_ch.TTXscaled,bin));
% cumhist.TTXCTZscaled = cumsum(histc(sampdata_ch.TTXCTZscaled,bin));
% plot(bin,cumhist.TTXscaled/max(cumhist.TTXscaled),'--m','LineWidth',2);
% plot(bin,cumhist.TTXCTZscaled/max(cumhist.TTXCTZscaled),'--g','LineWidth',2);

% duringCTZ vs preCTZ
plot(bin,cumhist.preCTZ/max(cumhist.preCTZ),'-k','LineWidth',2);
hold on;
plot(bin,cumhist.duringCTZ/max(cumhist.duringCTZ),'-r','LineWidth',2);
sampdata_ch.duringCTZscaled = (sampdata_ch.duringCTZ-s.duringCTZvspreCTZ.beta(1))/s.duringCTZvspreCTZ.beta(2);
cumhist.duringCTZscaled = cumsum(histc(sampdata_ch.duringCTZscaled,bin));
plot(bin,cumhist.duringCTZscaled/max(cumhist.duringCTZscaled),'--r','LineWidth',2);


set(gca,'LineWidth',0.5);
xlim([0 100]);
box off;
axis square;
set(gcf,'Position',[440 378 560 420]);

%% calculate stats

% % TTX vs Ctrl
% [h_ttest p_ttest] = ttest2(ctrlCellAvg,TTXCellAvg,0.05);
% [p_ranksum h_ranksum] = ranksum(ctrlCellAvg,TTXCellAvg);
% [h_ks p_ks] = kstest2(cumhist.ctrl/max(cumhist.ctrl),cumhist.TTXscaled/max(cumhist.TTXscaled));

% % CNQX vs Ctrl
% [h_ttest p_ttest] = ttest2(ctrlCellAvg,CNQXCellAvg,0.05);
% [p_ranksum h_ranksum] = ranksum(ctrlCellAvg,CNQXCellAvg);
% [h_ks p_ks] = kstest2(cumhist.ctrl/max(cumhist.ctrl),cumhist.CNQXscaled/max(cumhist.CNQXscaled));

% % CNQX vs CNQX+stim1X vs Ctrl
% [h_ttest_ctrlCNQX p_ttest_ctrlCNQX] = ttest2(ctrlCellAvg,CNQXCellAvg,0.05);
% [h_ttest_ctrlCNQXstim1X p_ttest_ctrlCNQXstim1X] = ttest2(ctrlCellAvg,CNQXstim1XCellAvg,0.05);
% [h_ttest_CNQXCNQXstim1X p_ttest_CNQXCNQXstim1X] = ttest2(CNQXCellAvg,CNQXstim1XCellAvg,0.05);
% [p_ranksum_ctrlCNQX h_ranksum_ctrlCNQX] = ranksum(ctrlCellAvg,CNQXCellAvg);
% [p_ranksum_ctrlCNQXstim1X h_ranksum_ctrlCNQXstim1X] = ranksum(ctrlCellAvg,CNQXstim1XCellAvg);
% [p_ranksum_CNQXCNQXstim1X h_ranksum_CNQXCNQXstim1X] = ranksum(CNQXCellAvg,CNQXstim1XCellAvg);
% [p_kw a_kw s_kw] = kruskalwallis([ctrlCellAvg;CNQXCellAvg;CNQXstim1XCellAvg],...
%     [repmat({'ctrl'},size(ctrlCellAvg));repmat({'CNQX'},size(CNQXCellAvg));repmat({'CNQXstim1X'},size(CNQXstim1XCellAvg))]);
% [p_anova a_anova s_anova] = anova1([ctrlCellAvg;CNQXCellAvg;CNQXstim1XCellAvg],...
%     [repmat({'ctrl'},size(ctrlCellAvg));repmat({'CNQX'},size(CNQXCellAvg));repmat({'CNQXstim1X'},size(CNQXstim1XCellAvg))]);
% % [p_kw a_kw s_kw] = kruskalwallis([sampdata_ch.ctrl;sampdata_ch.CNQX;sampdata_ch.CNQXstim1X],...
% %     [repmat({'ctrl'},size(sampdata_ch.ctrl));repmat({'CNQX'},size(sampdata_ch.CNQX));repmat({'CNQXstim1X'},size(sampdata_ch.CNQXstim1X))]);
% % [p_anova a_anova s_anova] = anova1([sampdata_ch.ctrl;sampdata_ch.CNQX;sampdata_ch.CNQXstim1X],...
% %     [repmat({'ctrl'},size(sampdata_ch.ctrl));repmat({'CNQX'},size(sampdata_ch.CNQX));repmat({'CNQXstim1X'},size(sampdata_ch.CNQXstim1X))]);
% [h_ks_ctrlCNQXscaled p_ks_ctrlCNQXscaled] = kstest2(cumhist.ctrl/max(cumhist.ctrl),cumhist.CNQXscaled/max(cumhist.CNQXscaled));
% [h_ks_ctrlCNQXstim1Xscaled p_ks_ctrlCNQXstim1Xscaled] = kstest2(cumhist.ctrl/max(cumhist.ctrl),cumhist.CNQXstim1Xscaled/max(cumhist.CNQXstim1Xscaled));
% [h_ks_CNQXCNQXstim1X p_ks_CNQXCNQXstim1X] = kstest2(cumhist.CNQX/max(cumhist.CNQX),cumhist.CNQXstim1X/max(cumhist.CNQXstim1X));

% % TTX vs TTXCTZ vs Ctrl
% [h_ttest_ctrlTTX p_ttest_ctrlTTX] = ttest2(ctrlCellAvg,TTXCellAvg,0.05);
% [h_ttest_ctrlTTXCTZ p_ttest_ctrlTTXCTZ] = ttest2(ctrlCellAvg,TTXCTZCellAvg,0.05);
% [h_ttest_TTXTTXCTZ p_ttest_TTXTTXCTZ] = ttest2(TTXCellAvg,TTXCTZCellAvg,0.05);
% [p_ranksum_ctrlTTX h_ranksum_ctrlTTX] = ranksum(ctrlCellAvg,TTXCellAvg);
% [p_ranksum_ctrlTTXCTZ h_ranksum_ctrlTTXCTZ] = ranksum(ctrlCellAvg,TTXCTZCellAvg);
% [p_ranksum_TTXTTXCTZ h_ranksum_TTXTTXCTZ] = ranksum(TTXCellAvg,TTXCTZCellAvg);
% [p_kw a_kw s_kw] = kruskalwallis([ctrlCellAvg;TTXCellAvg;TTXCTZCellAvg],...
%     [repmat({'ctrl'},size(ctrlCellAvg));repmat({'TTX'},size(TTXCellAvg));repmat({'TTXCTZ'},size(TTXCTZCellAvg))]);
% [p_anova a_anova s_anova] = anova1([ctrlCellAvg;TTXCellAvg;TTXCTZCellAvg],...
%     [repmat({'ctrl'},size(ctrlCellAvg));repmat({'TTX'},size(TTXCellAvg));repmat({'TTXCTZ'},size(TTXCTZCellAvg))]);
% % [p_kw a_kw s_kw] = kruskalwallis([sampdata_ch.ctrl;sampdata_ch.TTX;sampdata_ch.TTXCTZ],...
% %     [repmat({'ctrl'},size(sampdata_ch.ctrl));repmat({'TTX'},size(sampdata_ch.TTX));repmat({'TTXCTZ'},size(sampdata_ch.TTXCTZ))]);
% % [p_anova a_anova s_anova] = anova1([sampdata_ch.ctrl;sampdata_ch.TTX;sampdata_ch.TTXCTZ],...
% %     [repmat({'ctrl'},size(sampdata_ch.ctrl));repmat({'TTX'},size(sampdata_ch.TTX));repmat({'TTXCTZ'},size(sampdata_ch.TTXCTZ))]);
% [h_ks_ctrlTTXscaled p_ks_ctrlTTXscaled] = kstest2(cumhist.ctrl/max(cumhist.ctrl),cumhist.TTXscaled/max(cumhist.TTXscaled));
% [h_ks_ctrlTTXCTZscaled p_ks_ctrlTTXCTZscaled] = kstest2(cumhist.ctrl/max(cumhist.ctrl),cumhist.TTXCTZscaled/max(cumhist.TTXCTZscaled));
% [h_ks_TTXTTXCTZ p_ks_TTXTTXCTZ] = kstest2(cumhist.TTX/max(cumhist.TTX),cumhist.TTXCTZ/max(cumhist.TTXCTZ));
% [h_ks_TTXTTXCTZscaled p_ks_TTXTTXCTZscaled] = kstest2(cumhist.TTX/max(cumhist.TTX),cumhist.TTXCTZscaled/max(cumhist.TTXCTZscaled));
% [h_ks_TTXscaledTTXCTZscaled p_ks_TTXscaledTTXCTZscaled] = kstest2(cumhist.TTXscaled/max(cumhist.TTXscaled),cumhist.TTXCTZscaled/max(cumhist.TTXCTZscaled));

% duringCTZ vs preCTZ
[h_ttest p_ttest] = ttest2(preCTZCellAvg,duringCTZCellAvg,0.05);
[p_ranksum h_ranksum] = ranksum(preCTZCellAvg,duringCTZCellAvg);
[h_ks p_ks] = kstest2(cumhist.preCTZ/max(cumhist.preCTZ),cumhist.duringCTZscaled/max(cumhist.duringCTZscaled));


%% list values

% % TTX vs TTX+CTZ vs Ctrl
% z = [nanmean(ctrlCellAvg); ... %mean ctrl mEPSC feature
%     nanstd(ctrlCellAvg)/sqrt(length(ctrlCellAvg)); ...% stderr ctrl mEPSC feature
%     length(ctrlCellAvg); ... %number of ctrl samples
%     nanmean(TTXCellAvg); ... %mean TTX mEPSC feature
%     nanstd(TTXCellAvg)/sqrt(length(TTXCellAvg)); ...% stderr TTX mEPSC feature
%     length(TTXCellAvg); ... %number of TTX samples
%     nanmean(TTXCTZCellAvg); ... %mean TTXCTZ mEPSC feature
%     nanstd(TTXCTZCellAvg)/sqrt(length(TTXCTZCellAvg)); ...% stderr TTXCTZ mEPSC feature
%     length(TTXCTZCellAvg); ... %number of TTXCTZ samples
%     p_ttest_ctrlTTX; ...
%     p_ttest_ctrlTTXCTZ; ...
%     p_ttest_TTXTTXCTZ; ...
%     p_ranksum_ctrlTTX; ...
%     p_ranksum_ctrlTTXCTZ; ...
%     p_ranksum_TTXTTXCTZ; ...
%     nan; ...
%     p_anova;
%     p_kw;
%     nan; ...
%     s.TTXvsCtrl.beta(2); ... %slope of rank order plot linear fit
%     s.TTXvsCtrl.beta(1); ... %intercept of rank order plot linear fit
%     s.TTXvsCtrl.rsquare; ... %r^2 of rank order plot linear fit
%     s.TTXCTZvsCtrl.beta(2); ... %slope of rank order plot linear fit
%     s.TTXCTZvsCtrl.beta(1); ... %intercept of rank order plot linear fit
%     s.TTXCTZvsCtrl.rsquare; ... %r^2 of rank order plot linear fit
%     s.TTXCTZvsTTX.beta(2); ... %slope of rank order plot linear fit
%     s.TTXCTZvsTTX.beta(1); ... %intercept of rank order plot linear fit
%     s.TTXCTZvsTTX.rsquare; ... %r^2 of rank order plot linear fit
%     nan; ...
%     p_ks_ctrlTTXscaled; ...
%     p_ks_ctrlTTXCTZscaled; ...
%     p_ks_TTXTTXCTZ];


% % CNQX vs CNQX+stim vs Ctrl
% z = [nanmean(ctrlCellAvg); ... %mean ctrl mEPSC feature
%     nanstd(ctrlCellAvg)/sqrt(length(ctrlCellAvg)); ...% stderr ctrl mEPSC feature
%     length(ctrlCellAvg); ... %number of ctrl samples
%     nanmean(CNQXCellAvg); ... %mean CNQX mEPSC feature
%     nanstd(CNQXCellAvg)/sqrt(length(CNQXCellAvg)); ...% stderr CNQX mEPSC feature
%     length(CNQXCellAvg); ... %number of CNQX samples
%     nanmean(CNQXstim1XCellAvg); ... %mean CNQXstim1X mEPSC feature
%     nanstd(CNQXstim1XCellAvg)/sqrt(length(CNQXstim1XCellAvg)); ...% stderr CNQXstim1X mEPSC feature
%     length(CNQXstim1XCellAvg); ... %number of CNQXstim1X samples
%     p_ttest_ctrlCNQX; ...
%     p_ttest_ctrlCNQXstim1X; ...
%     p_ttest_CNQXCNQXstim1X; ...
%     p_ranksum_ctrlCNQX; ...
%     p_ranksum_ctrlCNQXstim1X; ...
%     p_ranksum_CNQXCNQXstim1X; ...
%     nan; ...
%     p_anova;
%     p_kw;
%     nan; ...
%     s.CNQXvsCtrl.beta(2); ... %slope of rank order plot linear fit
%     s.CNQXvsCtrl.beta(1); ... %intercept of rank order plot linear fit
%     s.CNQXvsCtrl.rsquare; ... %r^2 of rank order plot linear fit
%     s.CNQXstim1XvsCtrl.beta(2); ... %slope of rank order plot linear fit
%     s.CNQXstim1XvsCtrl.beta(1); ... %intercept of rank order plot linear fit
%     s.CNQXstim1XvsCtrl.rsquare; ... %r^2 of rank order plot linear fit
%     s.CNQXstim1XvsCNQX.beta(2); ... %slope of rank order plot linear fit
%     s.CNQXstim1XvsCNQX.beta(1); ... %intercept of rank order plot linear fit
%     s.CNQXstim1XvsCNQX.rsquare; ... %r^2 of rank order plot linear fit
%     nan; ...
%     p_ks_ctrlCNQXscaled; ...
%     p_ks_ctrlCNQXstim1Xscaled; ...
%     p_ks_CNQXCNQXstim1X];

% % TTX vs Ctrl
% z = [nanmean(ctrlCellAvg); ... %mean ctrl mEPSC feature
%     nanstd(ctrlCellAvg)/sqrt(length(ctrlCellAvg)); ...% stderr ctrl mEPSC feature
%     length(ctrlCellAvg); ... %number of ctrl samples
%     nanmean(TTXCellAvg); ... %mean TTX mEPSC feature
%     nanstd(TTXCellAvg)/sqrt(length(TTXCellAvg)); ...% stderr TTX mEPSC feature
%     length(TTXCellAvg); ... %number of TTX samples
%     p_ttest; ...
%     p_ranksum; ...
%     nan; ...
%     s.TTXvsCtrl.beta(2); ... %slope of rank order plot linear fit
%     s.TTXvsCtrl.beta(1); ... %intercept of rank order plot linear fit
%     s.TTXvsCtrl.rsquare; ... %r^2 of rank order plot linear fit
%     nan; ...
%     p_ks];

% % CNQX vs Ctrl
% z = [nanmean(ctrlCellAvg); ... %mean ctrl mEPSC feature
%     nanstd(ctrlCellAvg)/sqrt(length(ctrlCellAvg)); ...% stderr ctrl mEPSC feature
%     length(ctrlCellAvg); ... %number of ctrl samples
%     nanmean(CNQXCellAvg); ... %mean CNQX mEPSC feature
%     nanstd(CNQXCellAvg)/sqrt(length(CNQXCellAvg)); ...% stderr CNQX mEPSC feature
%     length(CNQXCellAvg); ... %number of CNQX samples
%     p_ttest; ...
%     p_ranksum; ...
%     nan; ...
%     s.CNQXvsCtrl.beta(2); ... %slope of rank order plot linear fit
%     s.CNQXvsCtrl.beta(1); ... %intercept of rank order plot linear fit
%     s.CNQXvsCtrl.rsquare; ... %r^2 of rank order plot linear fit
%     nan; ...
%     p_ks];

% duringCTZ vs preCTZ
z = [nanmean(preCTZCellAvg); ... %mean preCTZ mEPSC feature
    nanstd(preCTZCellAvg)/sqrt(length(preCTZCellAvg)); ...% stderr preCTZ mEPSC feature
    length(preCTZCellAvg); ... %number of preCTZ samples
    nanmean(duringCTZCellAvg); ... %mean duringCTZ mEPSC feature
    nanstd(duringCTZCellAvg)/sqrt(length(duringCTZCellAvg)); ...% stderr duringCTZ mEPSC feature
    length(duringCTZCellAvg); ... %number of duringCTZ samples
    p_ttest; ...
    p_ranksum; ...
    nan; ...
    s.duringCTZvspreCTZ.beta(2); ... %slope of rank order plot linear fit
    s.duringCTZvspreCTZ.beta(1); ... %intercept of rank order plot linear fit
    s.duringCTZvspreCTZ.rsquare; ... %r^2 of rank order plot linear fit
    nan; ...
    p_ks];