function stsd_grid_plotter_irreg_spaced(plain_extractedmatrix,conjoined_filename,hy,epoch_dividers,parameter_colors,stsd_parameters,number_of_IPIs,detection_criterion_matrix,fit_array)
%the goal here is to layout means and SDs of _o and _a vals (e.g., PSC_o and PSC_a) by interval, depending on whether release failed or succeeded
%this file and its callee stsd_grid_plotter_irreg_spaced_innerportion have been cafefully proofread, jan 3 2006
%making a lot of changes march 7 2006 to try to handle the three pulse trains, proofread again after changes march 14, 2006

%SD_based_defintion_of_failure=1; %to be meaningful, requires that Q_between_thresholds was active during call to create_storage_matrices
%simple_threshold_based_definition_of_failure=bitcmp(SD_based_defintion_of_failure,1);
failure_definition='template' %'SD' or 'threshold' or 'template'
f_s=8;   %font_size
f_w='demi'; %font_weight
addder=0    %set to zero to work with _o & _a pairs, set to 1 to work with _a & _b pairs  (the variable names below, e.g. 'trials_this_epoch_where_first_pulse_succeeded', have to be mentally translated)
text_interval=0;    %if 1, will superscribe the interval on the data points when plotting, otherwise will superscribe the epoch
use_values_from_curvefits=1;

if ~isempty(epoch_dividers),trials_this_epoch=[1:epoch_dividers(1)-.5];else trials_this_epoch=1:size(plain_extractedmatrix,2);end
for epoch=1:length(epoch_dividers)+1
    switch lower(failure_definition)
        case 'sd'
            trials_this_epoch_where_first_pulse_succeeded=trials_this_epoch(find(~isnan(plain_extractedmatrix(number_of_IPIs+1,trials_this_epoch,2))));    %# of SDs from baseline was set in create_storage_matrices
            trials_this_epoch_where_second_pulse_succeeded=trials_this_epoch(find(~isnan(plain_extractedmatrix(number_of_IPIs+2,trials_this_epoch,2))));
            trials_this_epoch_where_third_pulse_succeeded=trials_this_epoch(find(~isnan(plain_extractedmatrix(number_of_IPIs+3,trials_this_epoch,2))));
        case 'threshold'
            trials_this_epoch_where_first_pulse_succeeded=trials_this_epoch(find(plain_extractedmatrix(number_of_IPIs+1,trials_this_epoch,1)>3));  %if selecting by threshold, need to determine the threshold
            trials_this_epoch_where_second_pulse_succeeded=trials_this_epoch(find(plain_extractedmatrix(number_of_IPIs+2,trials_this_epoch,1)>3)); 
            trials_this_epoch_where_third_pulse_succeeded=trials_this_epoch(find(plain_extractedmatrix(number_of_IPIs+3,trials_this_epoch,1)>3));
        case 'template'
            detec_rows=size(detection_criterion_matrix,1);detec_seg=detec_rows/3;
            detection_criterion=1.5;
            trials_this_epoch_where_first_pulse_succeeded=trials_this_epoch(find(max(detection_criterion_matrix(1:detec_seg,trials_this_epoch))> detection_criterion))
            trials_this_epoch_where_second_pulse_succeeded=trials_this_epoch(find(max(detection_criterion_matrix(detec_seg+1:2*detec_seg,trials_this_epoch))>detection_criterion))
            trials_this_epoch_where_third_pulse_succeeded=trials_this_epoch(find(max(detection_criterion_matrix(2*detec_seg+1:end,trials_this_epoch))>detection_criterion))
    end
%     if SD_based_defintion_of_failure
%         trials_this_epoch_where_first_pulse_succeeded=trials_this_epoch(find(~isnan(plain_extractedmatrix(number_of_IPIs+1+addder,trials_this_epoch,2))));    %# of SDs from baseline was set in create_storage_matrices
%         trials_this_epoch_where_second_pulse_succeeded=trials_this_epoch(find(~isnan(plain_extractedmatrix(number_of_IPIs+2+addder,trials_this_epoch,2))));
% 	elseif simple_threshold_based_definition_of_failure
%         trials_this_epoch_where_first_pulse_succeeded=trials_this_epoch(find(plain_extractedmatrix(number_of_IPIs+1+addder,trials_this_epoch,1)>3));  %if selecting by threshold, need to determine the threshold
%         trials_this_epoch_where_second_pulse_succeeded=trials_this_epoch(find(plain_extractedmatrix(number_of_IPIs+2+addder,trials_this_epoch,1)>3)); 
%     end
    if isempty(epoch_dividers),epoch_shift=0;else epoch_shift=-1/6+((epoch-1)/(length(epoch_dividers)))*(2/6);end     %this is here to x-shift the plotted data by epoch a bit to make it more visible
    trials_this_epoch_where_first_pulse_failed=trials_this_epoch; trials_this_epoch_where_first_pulse_failed(trials_this_epoch_where_first_pulse_succeeded-(trials_this_epoch(1)-1))=[];
    handlez=[];     %used for optimizing the axes of subplots, when all is said and done
    for parameter=1:length(stsd_parameters)
        figure(50+parameter), set(gcf,'position',[1 31 1600 1094])
        for interval=1:length(hy)+1 %the +1 is for the _o values
            if interval<length(hy)+1,trials_this_epoch_having_this_interval=trials_this_epoch(find(plain_extractedmatrix(1,trials_this_epoch,parameter)==hy(interval)));
            else trials_this_epoch_having_this_interval=trials_this_epoch;end
            			            
            %First Column: all values of _o
			%mean&SD, all _a
			handlez(1,parameter)=subplot(3,3,1);title(strcat('Trials with resp-- ',num2str(1+addder),'--present or absent'),'fontsize',f_s),ylabel(strcat('Trials w resp-- ',num2str(2+addder),'--present or absent'),'fontsize',f_s)
            if interval>length(hy),valid_values=plain_extractedmatrix(number_of_IPIs+1+addder,trials_this_epoch,parameter);x_val=0;  %for _o values
            else valid_values=plain_extractedmatrix(number_of_IPIs+2+addder,trials_this_epoch_having_this_interval,parameter); x_val=log2(hy(interval));end     %for _a values
            stsd_grid_plotter_irreg_spaced_innerportion(x_val,valid_values,parameter_colors,parameter,epoch,epoch_shift,interval,text_interval,nan,f_s,f_w)
			
            %mean&SD, only including trials where _a was succesful
			handlez(4,parameter)=subplot(3,3,4);ylabel(strcat('Trials w resp-- ',num2str(2+addder),'--present'),'fontsize',f_s)
            if interval>length(hy),valid_values=plain_extractedmatrix(number_of_IPIs+1+addder,trials_this_epoch_where_second_pulse_succeeded,parameter);x_val=0;
            else trials_with_correct_interval_and_successful_second_pulse=[];for i=1:length(trials_this_epoch_having_this_interval),for j=1:length(trials_this_epoch_where_second_pulse_succeeded)
                        if trials_this_epoch_having_this_interval(i)==trials_this_epoch_where_second_pulse_succeeded(j)
                            trials_with_correct_interval_and_successful_second_pulse=[trials_with_correct_interval_and_successful_second_pulse trials_this_epoch_having_this_interval(i)];end,end,end
                valid_values=plain_extractedmatrix(number_of_IPIs+2+addder,trials_with_correct_interval_and_successful_second_pulse,parameter);x_val=log2(hy(interval));end
            stsd_grid_plotter_irreg_spaced_innerportion(x_val,valid_values,parameter_colors,parameter,epoch,epoch_shift,interval,text_interval,nan,f_s,f_w)
            
			%proportion of trials where the pulse was successful/total trials, for each interval
			handlez(7,parameter)=subplot(3,3,7);ylabel('Proportion successes','fontsize',f_s), xlabel('Log_2 IPI')
            if interval>length(hy),valid_values=length(trials_this_epoch_where_first_pulse_succeeded)/length(trials_this_epoch);x_val=0;  %for _o values
            else valid_values=length(trials_with_correct_interval_and_successful_second_pulse)/length(trials_this_epoch_having_this_interval);x_val=log2(hy(interval));end
            stsd_grid_plotter_irreg_spaced_innerportion(x_val,valid_values,parameter_colors,parameter,epoch,epoch_shift,interval,text_interval,length(trials_this_epoch),f_s,f_w)
            
            %Second Column: only when _o succeeds
            %mean&SD, all _a
            handlez(2,parameter)=subplot(3,3,2);title(strcat('Trials w resp-- ',num2str(1+addder),'--present'),'fontsize',f_s)
            if interval>length(hy),valid_values=plain_extractedmatrix(number_of_IPIs+1+addder,trials_this_epoch_where_first_pulse_succeeded,parameter);x_val=0;  %for _o values
            else trials_with_correct_interval_and_successful_first_pulse=[]; for i=1:length(trials_this_epoch_having_this_interval),for j=1:length(trials_this_epoch_where_first_pulse_succeeded)
                        if trials_this_epoch_having_this_interval(i)==trials_this_epoch_where_first_pulse_succeeded(j),
                            trials_with_correct_interval_and_successful_first_pulse=[trials_with_correct_interval_and_successful_first_pulse trials_this_epoch_having_this_interval(i)];end,end,end
                valid_values=plain_extractedmatrix(number_of_IPIs+2+addder,trials_with_correct_interval_and_successful_first_pulse,parameter);x_val=log2(hy(interval));end
            stsd_grid_plotter_irreg_spaced_innerportion(x_val,valid_values,parameter_colors,parameter,epoch,epoch_shift,interval,text_interval,nan,f_s,f_w)
            
			%mean&SD, successes of _a, no failures of _a
			handlez(5,parameter)=subplot(3,3,5);
            if interval>length(hy),first_succeeds_and_second_succeeds_regardless_of_interval=[];
                for i=1:length(trials_this_epoch_where_first_pulse_succeeded), for j=1:length(trials_this_epoch_where_second_pulse_succeeded)
                        if trials_this_epoch_where_first_pulse_succeeded(i)==trials_this_epoch_where_second_pulse_succeeded(j),
                            first_succeeds_and_second_succeeds_regardless_of_interval=[first_succeeds_and_second_succeeds_regardless_of_interval trials_this_epoch_where_first_pulse_succeeded(i)];end,end,end
                 valid_values=plain_extractedmatrix(number_of_IPIs+1+addder,first_succeeds_and_second_succeeds_regardless_of_interval,parameter);x_val=0;
            else trials_with_correct_interval_and_both_pulses_success=[];
                for i=1:length(trials_with_correct_interval_and_successful_second_pulse),for j=1:length(trials_this_epoch_where_first_pulse_succeeded),
                        if trials_with_correct_interval_and_successful_second_pulse(i)==trials_this_epoch_where_first_pulse_succeeded(j),
                            trials_with_correct_interval_and_both_pulses_success=[trials_with_correct_interval_and_both_pulses_success trials_with_correct_interval_and_successful_second_pulse(i)];end,end,end 
                valid_values=plain_extractedmatrix(number_of_IPIs+2+addder,trials_with_correct_interval_and_both_pulses_success,parameter);x_val=log2(hy(interval));end
            stsd_grid_plotter_irreg_spaced_innerportion(x_val,valid_values,parameter_colors,parameter,epoch,epoch_shift,interval,text_interval,nan,f_s,f_w)

			%proportion successes
			handlez(8,parameter)=subplot(3,3,8);xlabel('Log_2 IPI')
            if interval>length(hy),valid_values=1;x_val=0;  %for _o values
            else    trials_with_first_pulse_success_then_correct_interval=[];for i=1:length(trials_this_epoch_where_first_pulse_succeeded), for j=1:length(trials_this_epoch_having_this_interval),
                        if trials_this_epoch_where_first_pulse_succeeded(i)==trials_this_epoch_having_this_interval(j),
                            trials_with_first_pulse_success_then_correct_interval=[trials_with_first_pulse_success_then_correct_interval trials_this_epoch_where_first_pulse_succeeded(i)];end,end,end
                valid_values=length(trials_with_correct_interval_and_both_pulses_success)/length(trials_with_first_pulse_success_then_correct_interval);x_val=log2(hy(interval));end
            stsd_grid_plotter_irreg_spaced_innerportion(x_val,valid_values,parameter_colors,parameter,epoch,epoch_shift,interval,text_interval,length(trials_this_epoch_where_first_pulse_succeeded),f_s,f_w)
            
            %Third Column: only when _o fails
            %mean&SD, all _a
            handlez(3,parameter)=subplot(3,3,3);title(strcat('Trials w resp-- ',num2str(1+addder),'--absent'),'fontsize',f_s)
            if interval>length(hy),valid_values=plain_extractedmatrix(number_of_IPIs+1+addder,trials_this_epoch_where_first_pulse_failed,parameter);x_val=0;  %for _o values
            else trials_with_first_pulse_failure_then_correct_interval=[];for i=1:length(trials_this_epoch_having_this_interval),for j=1:length(trials_this_epoch_where_first_pulse_failed)
                        if trials_this_epoch_having_this_interval(i)==trials_this_epoch_where_first_pulse_failed(j)
                            trials_with_first_pulse_failure_then_correct_interval=[trials_with_first_pulse_failure_then_correct_interval trials_this_epoch_having_this_interval(i)];end,end,end
                valid_values=plain_extractedmatrix(number_of_IPIs+2+addder,trials_with_first_pulse_failure_then_correct_interval,parameter);x_val=log2(hy(interval));end
            stsd_grid_plotter_irreg_spaced_innerportion(x_val,valid_values,parameter_colors,parameter,epoch,epoch_shift,interval,text_interval,nan,f_s,f_w)
			
			%mean&SD, successes only of _a
			handlez(6,parameter)=subplot(3,3,6);
            if interval>length(hy),trials_any_interval_first_pulse_fails_second_succeeds=[];
                for i=1:length(trials_this_epoch_where_first_pulse_failed),for j=1:length(trials_this_epoch_where_second_pulse_succeeded)
                        if trials_this_epoch_where_first_pulse_failed(i)==trials_this_epoch_where_second_pulse_succeeded(j),
                            trials_any_interval_first_pulse_fails_second_succeeds=[trials_any_interval_first_pulse_fails_second_succeeds trials_this_epoch_where_first_pulse_failed(i)];end,end,end
                valid_values=plain_extractedmatrix(number_of_IPIs+1+addder,trials_any_interval_first_pulse_fails_second_succeeds,parameter);x_val=0;  %for _o values
            else trials_with_correct_interval_first_pulse_fails_second_succeeds=[];
                for i=1:length(trials_with_correct_interval_and_successful_second_pulse),for j=1:length(trials_this_epoch_where_first_pulse_failed),
                        if trials_with_correct_interval_and_successful_second_pulse(i)==trials_this_epoch_where_first_pulse_failed(j)
                            trials_with_correct_interval_first_pulse_fails_second_succeeds=[trials_with_correct_interval_first_pulse_fails_second_succeeds trials_with_correct_interval_and_successful_second_pulse(i)];end,end,end
                valid_values=plain_extractedmatrix(number_of_IPIs+2+addder,trials_with_correct_interval_first_pulse_fails_second_succeeds,parameter);x_val=log2(hy(interval));end
            stsd_grid_plotter_irreg_spaced_innerportion(x_val,valid_values,parameter_colors,parameter,epoch,epoch_shift,interval,text_interval,nan,f_s,f_w)
			
			%proportion successes
			handlez(9,parameter)=subplot(3,3,9);xlabel('Log_2 IPI')
            if interval>length(hy),valid_values=0;x_val=0;  %for _o values
            else valid_values=length(trials_with_correct_interval_first_pulse_fails_second_succeeds)/length(trials_with_first_pulse_failure_then_correct_interval);x_val=log2(hy(interval));end
            stsd_grid_plotter_irreg_spaced_innerportion(x_val,valid_values,parameter_colors,parameter,epoch,epoch_shift,interval,text_interval,length(trials_this_epoch_where_first_pulse_failed),f_s,f_w)
            
            if epoch==length(epoch_dividers)+1 & interval==length(hy)+1  %this is all about getting the axes aligned -- you know, for comparison?
                set(handlez(1:9,parameter),'xlim',[min(min(cell2mat(get(handlez(1:9,parameter),'xlim')))) max(max(cell2mat(get(handlez(1:9,parameter),'xlim'))))]);
                set(handlez(1:6,parameter),'ylim',[min(min(cell2mat(get(handlez(1:6,parameter),'ylim')))) max(max(cell2mat(get(handlez(1:6,parameter),'ylim'))))]);
                set(handlez(7:9,parameter),'ylim',[min(min(cell2mat(get(handlez(7:9,parameter),'ylim')))) max(max(cell2mat(get(handlez(7:9,parameter),'ylim'))))]);
                
                subplot(3,3,1),text(min(min(cell2mat(get(handlez(1:6,parameter),'xlim')))),max(max(cell2mat(get(handlez(1:6,parameter),'ylim')))),stsd_parameters{parameter},'fontsize',20,'fontweight','bold')               
            end
        end
	end
    if length(epoch_dividers)>epoch,trials_this_epoch=[epoch_dividers(epoch)+.5 :epoch_dividers(epoch+1)-.5];
    elseif length(epoch_dividers)==epoch,trials_this_epoch=[epoch_dividers(epoch)+.5:size(plain_extractedmatrix,2)];
    end
end