function mean_and_error_matrix=analyze_and_plot_mean_responses(values_preinduc, values_postinduc, critlogical_preinduc, critlogical_postinduc, casecolor, x_offset,...
    do_rungs_plot_not_double_errobar_plot, use_first_val_only, figaddon);
%description: takes uploaded psc values or charge values calculated by careful_charge_calculator, finds the mean and confidence interval before and after induction, plots the mean and error bar in the style instructed
%called by: analyze_across_expts, analyze_across_sd_stdp_expts(deprecated), analyze_waveforms_clusters_mixtures_using_database(deprecated)
%calls: agresti_coull_binomial_confidence_interval, generalized_rung_plotter, double_errorbar_plotter, standard_err
%notes: potency on trials with all failed responses is undefined;  such trials are removed prior to plotting.  same for 'failpotency' on trials with no failed responses....
 %agresti-coull binomial CI is used for reliability when only the first pulse is considered, a binomial situation.  otherwise standard error is used with reliability as...
 %it is already a mean, and standard error is used with potency, also a mean.
%code confirmed date: oct 8, 2009

if use_first_val_only   %only the first value, value_o, is considered from each trial
    values_antes=values_preinduc(1,:);values_despues=values_postinduc(1,:);critlogical_antes=critlogical_preinduc(1,:);critlogical_despues=critlogical_postinduc(1,:);
    values_succ_antes=values_antes(critlogical_antes);values_succ_despues=values_despues(critlogical_despues);
	values_fail_antes=values_antes(~critlogical_antes);values_fail_despues=values_despues(~critlogical_despues);
    
	%proportion of successes of event_o with binomial confidence interval
	numsuccantes=length(values_succ_antes);      numtrialsantes=length(values_antes);
	numsuccdespues=length(values_succ_despues);     numtrialsdespues=length(values_despues);
	succpropantes=numsuccantes/numtrialsantes;succpropdespues=numsuccdespues/numtrialsdespues;
	[upperlimbefore,lowerlimbefore]=agresti_coull_binomial_confidence_interval(numsuccantes,numtrialsantes);
	[upperlimafter,lowerlimafter]=agresti_coull_binomial_confidence_interval(numsuccdespues,numtrialsdespues);
else
	num_APs=size(critlogical_preinduc,1)
    successes_per_trial_preinduc=sum(critlogical_preinduc);successes_per_trial_postinduc=sum(critlogical_postinduc);
    reliability_preinduc=successes_per_trial_preinduc/num_APs;  reliability_postinduc=successes_per_trial_postinduc/num_APs;
	mean_values_preinduc=sum(values_preinduc)/num_APs;  mean_values_postinduc=sum(values_postinduc)/num_APs;
    values_for_potency_preinduc=values_preinduc; values_for_potency_preinduc(~critlogical_preinduc)=0;
    values_for_potency_postinduc=values_postinduc; values_for_potency_postinduc(~critlogical_postinduc)=0;
    potencies_preinduc=sum(values_for_potency_preinduc)./successes_per_trial_preinduc;potencies_postinduc=sum(values_for_potency_postinduc)./successes_per_trial_postinduc;
    potencies_preinduc(isnan(potencies_preinduc))=[];  potencies_postinduc(isnan(potencies_postinduc))=[];
    
    values_for_failpot_preinduc=values_preinduc; values_for_failpot_preinduc(critlogical_preinduc)=0; 
    values_for_failpot_postinduc=values_postinduc; values_for_failpot_postinduc(critlogical_postinduc)=0;
    failpots_preinduc=sum(values_for_failpot_preinduc)./sum(~critlogical_preinduc); failpots_postinduc=sum(values_for_failpot_postinduc)./sum(~critlogical_postinduc);
    failpots_preinduc(isnan(failpots_preinduc))=[]; failpots_postinduc(isnan(values_for_failpot_postinduc))=[];
     
    values_antes=mean_values_preinduc;values_despues=mean_values_postinduc;    
    values_succ_antes=potencies_preinduc;values_succ_despues=potencies_postinduc;
    values_fail_antes=failpots_preinduc;values_fail_despues=failpots_postinduc;
            
    succpropantes=mean(reliability_preinduc);
    upperlimbefore=succpropantes+standard_err(reliability_preinduc);
    lowerlimbefore=succpropantes-standard_err(reliability_preinduc);
    succpropdespues=mean(reliability_postinduc);
    upperlimafter=succpropdespues+standard_err(reliability_postinduc);
    lowerlimafter=succpropdespues-standard_err(reliability_postinduc);
end

%mean with SEM
meanbefore=mean(values_antes);stdbefore=std(values_antes);sembefore=stdbefore/sqrt(length(values_antes));
meanafter=mean(values_despues);stdafter=std(values_despues);semafter=stdafter/sqrt(length(values_despues));

%mean, successes only, with SEM
meansuccbefore=mean(values_succ_antes);stdsuccbefore=std(values_succ_antes);semsuccbefore=stdsuccbefore/sqrt(length(values_succ_antes));
meansuccafter=mean(values_succ_despues);stdsuccafter=std(values_succ_despues);semsuccafter=stdsuccafter/sqrt(length(values_succ_despues));

%mean, failures only, with SEM
meanfailbefore=mean(values_fail_antes);stdfailbefore=std(values_fail_antes);semfailbefore=stdfailbefore/sqrt(length(values_fail_antes));
meanfailafter=mean(values_fail_despues);stdfailafter=std(values_fail_despues);semfailafter=stdfailafter/sqrt(length(values_fail_despues));

figure(1+figaddon),hold on, title('value')
if do_rungs_plot_not_double_errobar_plot
    subplot(3,3,[2 5 8]),set(gcf,'position',[145   120   278   707]);generalized_rung_plotter([meanbefore meanafter],[meanbefore+sembefore meanafter+semafter],[meanbefore-sembefore meanafter-semafter],casecolor,x_offset)
else
    double_errorbar_plotter_dwn([meanbefore,sembefore,meanafter,semafter],casecolor)
end

figure(2+figaddon),hold on, title('potency')
if do_rungs_plot_not_double_errobar_plot
    subplot(3,3,[2 5 8]),set(gcf,'position',[445   120   278   707]);generalized_rung_plotter([meansuccbefore meansuccafter],[meansuccbefore+semsuccbefore meansuccafter+semsuccafter],[meansuccbefore-semsuccbefore meansuccafter-semsuccafter],casecolor,x_offset)
else
    double_errorbar_plotter_dwn([meansuccbefore,semsuccbefore,meansuccafter,semsuccafter],casecolor)
end

figure(3+figaddon),hold on, title('reliability')
if do_rungs_plot_not_double_errobar_plot
    subplot(3,3,[2 5 8]),set(gcf,'position',[745   120   278   707]),generalized_rung_plotter([succpropantes succpropdespues],[upperlimbefore upperlimafter],[lowerlimbefore lowerlimafter],casecolor,x_offset);
else
    double_errorbar_plotter_dwn([succpropantes,upperlimbefore,lowerlimbefore,succpropdespues,upperlimafter,lowerlimafter],casecolor)
end
    
figure(4+figaddon),hold on, title('failpotency')
if do_rungs_plot_not_double_errobar_plot
    subplot(3,3,[2 5 8]),set(gcf,'position',[1045   120   278   707]),generalized_rung_plotter([meanfailbefore meanfailafter],[meanfailbefore+semfailbefore meanfailafter+semfailafter],[meanfailbefore-semfailbefore meanfailafter-semfailafter],casecolor,x_offset)
else
    double_errorbar_plotter_dwn([meanfailbefore,semfailbefore,meanfailafter,semfailafter],casecolor)
end

mean_and_error_matrix=[meanbefore,sembefore,meanafter,semafter,meansuccbefore,semsuccbefore,meansuccafter,semsuccafter,succpropantes,upperlimbefore,lowerlimbefore,succpropdespues,upperlimafter,lowerlimafter,meanfailbefore,semfailbefore,meanfailafter,semfailafter]';