function analyze_history_dependence(pscmat,critz,array_of_charges,trialcount_mat,livetrials_preinduc,livetrials_postinduc,waveform_matrix,casecolor,current_expt,number_of_APs,patchtitle)
%description: designed for testing the hypothesis that the nth PSC in a train is influenced by IPIs earlier than IPIn-1  (the effect of IPIn on the nth PSC being STSD)
%called by: analyze_across_sd_stdp_expts, 
%calls: agresti_coull_binomial_confidence_interval, generalized_rung_plotter, cumulative_histogram...

historydep_preinduconly=1
if historydep_preinduconly
    livetrials=livetrials_preinduc;
    label_addendum='preinduction';
    alpha_type=50;  %the duration in ms of the IPI in question -- for 3 pulse trains, IPIa, for 4 pulse trains, IPIb
    beta_type=400;  %duration of the second group of IPIs
end

linewidth=1;
plot_waveforms=1;
	plot_colorbars=0;

    zaz=size(waveform_matrix)
    taz=livetrials
    
if number_of_APs==3, keyIPIrow=1;elseif number_of_APs==4,keyIPIrow=2;end
liveIPIs=pscmat(keyIPIrow,livetrials)
livewaveforms=waveform_matrix(:,livetrials);
livewaveforms_typealpha=livewaveforms(:,find(liveIPIs==alpha_type))';
livewaveforms_typebeta=livewaveforms(:,find(liveIPIs==beta_type))';
livecritz=critz(end,livetrials);
livecritz_typealpha=livecritz(find(liveIPIs==alpha_type));
livecritz_typebeta=livecritz(find(liveIPIs==beta_type));
livecharges=array_of_charges(end,livetrials);
livecharges_typealpha=livecharges(find(liveIPIs==alpha_type));
livecharges_typebeta=livecharges(find(liveIPIs==beta_type));
livepscs=pscmat(end,livetrials);
livepscs_typealpha=livepscs(find(liveIPIs==alpha_type));
livepscs_typebeta=livepscs(find(liveIPIs==beta_type));

figure(100+current_expt), hold on,title(patchtitle)
cumulative_histogram(livecharges_typealpha,'c')
cumulative_histogram(livecharges_typebeta,'m:')
 if any(~isnan(livecharges_typealpha))&&any(~isnan(livecharges_typebeta)),[h,p]=kstest2(livecharges_typealpha,livecharges_typebeta);else p='none';end
ylabel(strcat('KS p:',num2str(p))),xlabel(strcat('Final charge,',num2str(alpha_type),'versus',num2str(beta_type),label_addendum))

figure(200+current_expt),hold on, title(patchtitle)
cumulative_histogram(livepscs_typealpha,'c')
cumulative_histogram(livepscs_typebeta,'m:')
if any(~isnan(livepscs_typealpha))&&any(~isnan(livepscs_typebeta)),[h,p]=kstest2(livepscs_typealpha,livepscs_typebeta);else p='none';end
ylabel(strcat('KS p:',num2str(p))),xlabel(strcat('Final PSC,',num2str(alpha_type),'versus',num2str(beta_type),label_addendum))

figure(300+current_expt),hold on, title(patchtitle)
alpha_succ_count=length(find(livecritz_typealpha));      alpha_length=length(livecritz_typealpha);
beta_succ_count=length(find(livecritz_typebeta));      beta_length=length(livecritz_typebeta);
prop_alpha_succ=alpha_succ_count/alpha_length;prop_beta_succ=beta_succ_count/beta_length;
[upperlimalpha,lowerlimalpha]=agresti_coull_binomial_confidence_interval(alpha_succ_count,alpha_length);
[upperlimbeta,lowerlimbeta]=agresti_coull_binomial_confidence_interval(beta_succ_count,beta_length);
generalized_rung_plotter([prop_alpha_succ prop_beta_succ],[upperlimalpha upperlimbeta],[lowerlimalpha lowerlimbeta],[.2 1 .2],0)
xlabel(strcat('HD of Pr,',num2str(alpha_type),'versus',num2str(beta_type),label_addendum))

if plot_waveforms
    clr=colormap(jet(length(livetrials)));clr_alpha=clr(find(liveIPIs==alpha_type),:);clr_beta=clr(find(liveIPIs==beta_type),:);
    
    figure(400+current_expt),title(patchtitle),subplot(10,10,[1:80]),hold on
	for alphaz=1:size(livewaveforms_typealpha,1),plot(livewaveforms_typealpha(alphaz,:),'color',clr_alpha(alphaz,:),'linewidth',linewidth),end
	if plot_colorbars,subplot(10,10,[91:100]),imagesc([1:length(livetrials)]),set(gca,'xtick',[]),end
	
	figure(500+current_expt),title(patchtitle),subplot(10,10,[1:80]),hold on
	for betaz=1:size(livewaveforms_typebeta,1),plot(livewaveforms_typebeta(betaz,:),'color',clr_beta(betaz,:),'linewidth',linewidth),end
	if plot_colorbars,subplot(10,10,[91:100]),imagesc([1:length(livetrials)]),set(gca,'xtick',[]),end
end