%clearvars -except dingbat4*;
%clear all;

%load('C:\Users\mingfaifong\Documents\2012-08-08_dingbat4-dingbat3_patch-analysis\dingbat4_alldata.mat');

% figure(3);

dat_remove = [];

for i = 1:length(dat)
    if size(dat{i},1) < 20
        dat_remove = [dat_remove i];
    end
end
dat(dat_remove,:) = [];

dat_tmp = [dat_tmp; dat];

%%
edges = 0:0.5:300;
%edges = 0:1:300;

for i = 1:length(dat)
    x(:,i) = histc(dat{i}(:,3),edges);
    y(:,i) = x(:,i)/size(dat{i},1);
%     subplot(length(dat),1,i);
%     bar(edges+0.25,x(:,i));
%     axis([0 40 0 10]);
end
% xlabel('mEPSC amplitude (pA)');


%%
% [sigma_x mu_x] = gaussfit(edges+0.25,sum(x'));
% [sigma_y mu_y] = gaussfit(edges+0.25,sum(y'));

[sigma_x mu_x A_x] = mygaussfit(edges+0.25,sum(x'),0.25);
[sigma_y mu_y A_y] = mygaussfit(edges+0.25,sum(y'),0.25);



%% plots
figure(3);
subplot(2,1,1)
hold off;
bar(edges+0.25,x,'stacked')
% hold on;
% xfit = gaussmf(edges+0.25,[sigma_x mu_x])*A_x;
% plot(edges+0.25,xfit,'-k');
xlim([0 75]);
set(gca,'FontSize',14,'XTick',[0:5:75])
xlabel('mEPSC amplitude (pA)');
ylabel(['Number of Events',10,'(raw counts)']);
title(['TTX+CTZ Amplitude Histogram (',num2str(length(dat)),' cells)'],'fontsize',14)
grid on;

subplot(2,1,2)
hold off;
bar(edges+0.25,y,'stacked');
% hold on;
% yfit = gaussmf(edges+0.25,[sigma_y mu_y])*A_y;
% plot(edges+0.25,yfit,'-k');
xlim([0 75]);
set(gca,'FontSize',14,'XTick',[0:5:75])
xlabel('mEPSC amplitude (pA)');
ylabel(['Number of Events',10,'(normalized to total events per cell)']);
grid on