function general_info_files=analyze_across_expts(filename_info_for_query,query_style)
%description: queries the sql database and returns information on files matching the selection pattern;  loads the extracted data for each
 %of these experiments, and runs analyses as directed by flags set at the beginning of the file.
%called by: command line
%calls: query_generator, retrieve_data_from_sql_database, retrieve_extracted_data, find_detection_crits_and_latencies_generalizedform,
%find_time_windows, simpleteetester, analyze_and_plot_mean_responses, ...
%code confirmed date: 

clearvars -except filename_info_for_query query_style
close all,warning off MATLAB:divideByZero,tic

lookat_preinduc_ao_induc_ao_postinduc=7;  %1 is preinduc only, 2 is induc only, 3 is postinduc only, 7 is all three
interval_selection_method=3;   %interval_selection_method 1 is use all pre and postinduction trials, 2 is use the general window for all cases, 3 is use from the beginning of the preinduc spec window to end of the postinduc specific window contiguously, 4 is use the preinduc specific window and the postinduc specific window without using the postinduction portion prior to the specific window
	general_time_window_before=[-10 0];   %in minutes
	general_time_window_after=[15 25];  %in minutes
		ensure_equal_lengths=1;
ttest_psco_yesno=0;
    ttestpsc_firstorall=1;       %1=first psc, 2=sum of all pscs
    writettestresulttodatabase_yesno=0;
analyze_and_plot_mean_responses_psc_yesno=0;
    do_rungs_plot_not_double_errobar_plot_psc=0;
    use_first_val_only_psc=0;
    figaddon_pscmeans=60;
analyze_correlation_between_successive_pscs_yesno=0;
    psccorrelation_figaddon=70;
analyze_autocorrelation_psc_yesno=0;
    pscautocorr_figaddon=100;
    use_first_val_only_pscautocorr=0;
analyze_latencies_yesno=0;
    latency_figaddon=200;
    latencies_by_epoch=0;
    latencies_by_AP=1; 
    latencies_by_contingency=1;
careful_asynch_measurement_yesno=0;
careful_charge_calculator_yesno=0;
    analyze_and_plot_mean_responses_charge_yesno=0;
        do_rungs_plot_not_double_errobar_plot_charge=0;
        use_first_val_only_charge=1;
        figaddon_chargemeans=70;
    carefulcharge_interval_selection_method_range=interval_selection_method;%1:5
    first_waveform_only_yesno=0;
    success_waveforms_only_yesno=0;%0:1
    activate_local_offset_correction=0;
  analyze_release_dependence_yesno=1;
  analyze_history_dependence_yesno=0;
  analyze_size_distributions_yesno=0;
  analyze_sd_over_time_yesno=0;
    use_psc_instead_of_charge_sdovertime=1;
  analyze_diary_yesno=0;
    use_psc_instead_of_charge_diary=1;
  analyze_realtime_yesno=0;
  analyze_correlation_between_successive_charges_yesno=0;
    chargecorrelation_figaddon=150;
  analyze_autocorrelation_charge_yesno=0;
    chargeautocorr_figaddon=160;
changepoints_to_find=[];
    num_bootstrap_runs=10;
    num_permutations=10;
    cp_ID_method=2;
    bootstrap_style=2;
    save_changepoint_matrix=0;
batch_exporting=0;
instant_archiving=0;
general_figure_position=[30 40 1200 850];
close_figs_after_each_loop=0;
override_colors=0;     %override the color assigned to the case in the database
override_threshold=[0 2];  %override the criterion for event detection (first element) to use a set threshold for all cases (second element)

successes=[ 1     5     8    10    12    14     15     25    26    30];     %of expts in the database, these ones successfully potentiated
failures=[2     7     9    27    28];
%succs_and_fails=[1     5     8    10    12    14     15     25    26    30 2     7     9    27    28];
succs_and_fails=[1 2 5 7 8 9 10 12 14 15 25 26 27 28 30];

casecheck=successes; 

%------------

%generating and executing the query to retrieve the appropriate filenames and/or other file info
%for query_style 1, filename_info_for_query; for query_style 2 filename_info_for_query should be a text string with fragment of the file name; for query_style 3, 
%filename_info_for_query ; for query_style 4 filename_info_for_query should be a cell array of file names
if nargin==3
    selection_clauses=query_generator(query_style,filename_info_for_query,query_suffix);
elseif nargin==2
    selection_clauses=query_generator(query_style,filename_info_for_query,'[]');
elseif nargin==1
    selection_clauses=query_generator(3,filename_info_for_query,'[]');   
elseif nargin==0
    selection_clauses=query_generator(3,'','[]');
end
[preinduction_files]=retrieve_data_from_sql_database(selection_clauses{1})
npre=size(preinduction_files,1);
if length(selection_clauses)>1 & lookat_preinduc_ao_induc_ao_postinduc>1
    [induction_files]=retrieve_data_from_sql_database(selection_clauses{2})
    [postinduction_files]=retrieve_data_from_sql_database(selection_clauses{3})
    nind=size(induction_files,1);npost=size(postinduction_files,1);
    if any(npre~=[nind npost]),a=npre, b=nind,c=npost,error('filesizemismatch'),end
else 
    induction_files=preinduction_files;postinduction_files=preinduction_files;  %this ridiculous hack is the most expedient solution at the moment, sadly
end

%loop for loading and analysis of the data contained in the files with the names just retrieved
tooshorttomeasurenames=[];
significantlydifferentbefafnames=[];
%summed_slopes_each_induction=[];
%charge_information_vector=[];
cp_output_mat=[];
%indiv_charges_by_sweep_preinduction=[];
%indiv_charges_by_sweep_postinduction=[];
%mean_sem_induc_matrix=[];
%mean_asym_sem_induc_matrix=[];
asynch_event_data_holder=[];
bgrd_event_data_holder=[];
%case_numbers_and_trialcountmats=[];
charge_p_value_holder=[];
psc_p_value_holder=[];
%sd_ratio_diff_times=[];
%minutes_per_trial_each_case=[];
mean_charge_and_error_matrices=[];
mean_psc_and_error_matrices=[];
%criterion_each_case=[];
sd_change_info_holder=[];
%array_of_charges_fullrange_cell_array={};
%rawmat_cell_array={};
%critz_cell_array={};
%mean_induction_traces={};
%slopes_each_induction={};
individual_induction_traces_all_expts={};
%pscmat_cell_array={};
%special_waveforms_cell_array={};
difftimes=[];
RD_fisher_matrix2trig={};

for presentcase=1:npre
    if any(presentcase==casecheck)       %if any of the present expts match those indicated in casecheck
        disp(strcat('Currently analyzing case #',num2str(presentcase)))
        x_offset=(.5-rand)/4;   %this provides an offset so individual experiments should be more visible if rung plots are generated
        
        [pscs_fullepoch_preinduc, pscs_fullepoch_postinduc, criteria_fullepoch_preinduc, criteria_fullepoch_postinduc,asynchtraces_fullepoch_preinduc, asynchtraces_fullepoch_postinduc,...
        asynchcriteria_fullepoch_preinduc, asynchcriteria_fullepoch_postinduc,rawtraces_fullepoch_preinduc,rawtraces_fullepoch_postinduc, preind_presyn_pulse_duration,...
        postind_presyn_pulse_duration, raw_induction_trace, induction_peaks, induction_pulse_duration, induction_sampling_rate, patchtitle, general_info_files, casecolor]=...
        retrieve_extracted_data(lookat_preinduc_ao_induc_ao_postinduc,careful_charge_calculator_yesno,careful_asynch_measurement_yesno,presentcase,preinduction_files,...
        induction_files,postinduction_files);
    
        if override_colors
            clr=colors_for_stdp_expts(length(casecheck)); casecolor=clr(find(presentcase==casecheck),:);%overriding the colors from the database to use an hsv colormap
        end
        if override_threshold(1)
            general_info_files{presentcase,4}=override_threshold(2); disp('override threshold')    %overriding the threshold setting from the database
        end
         
        if find(presentcase==[27 28])
            general_info_files{presentcase,4}=.4%manually lowering the threshold for two stdp failure expts
        end
        
        %criterion_each_case=[criterion_each_case general_info_files{presentcase,4}];
        %minutes_per_trial_each_case=[minutes_per_trial_each_case general_info_files{presentcase,5}];
        
    
        numtrials_fullepoch_preinduc=size(pscs_fullepoch_preinduc,2); numtrials_fullepoch_postinduc=size(pscs_fullepoch_postinduc,2);
        trialvec_fullepoch_preinduc=1:numtrials_fullepoch_preinduc; trialvec_fullepoch_postinduc=1:numtrials_fullepoch_postinduc;
        minutes_per_trial=general_info_files{presentcase,5};
        timevec_fullepoch_preinduc=[-1*round(numtrials_fullepoch_preinduc*minutes_per_trial) 0];timevec_fullepoch_postinduc=[1 round(numtrials_fullepoch_postinduc*minutes_per_trial)];
        
        IPIarray_fullepoch_prepostinduc=[pscs_fullepoch_preinduc pscs_fullepoch_postinduc];
        IPIarray_fullepoch_prepostinduc=IPIarray_fullepoch_prepostinduc(1:2,:);
        
        %finding the detection criterion and latency matrices
        if any(lookat_preinduc_ao_induc_ao_postinduc==[1 7])
            [latencies_fullepoch_preinduc,critlogical_fullepoch_preinduc]=find_detection_crits_and_latencies_generalizedform(criteria_fullepoch_preinduc,general_info_files{presentcase,4},general_info_files{presentcase,6});
        else
            latencies_fullepoch_preinduc=[];critlogical_fullepoch_preinduc=[];
        end
        if any(lookat_preinduc_ao_induc_ao_postinduc==[3 7])
            [latencies_fullepoch_postinduc,critlogical_fullepoch_postinduc]=find_detection_crits_and_latencies_generalizedform(criteria_fullepoch_postinduc,general_info_files{presentcase,4},general_info_files{presentcase,6});
        else
            latencies_fullepoch_postinduc=[];critlogical_fullepoch_postinduc=[];
        end
        
        %finding the time windows -- note that the returned PSC arrays still contain the records of IPIs in the top two rows
        if any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7])
            [pscs_liveinterval_preinduc,pscs_liveinterval_postinduc,numtrials_liveinterval_preinduc,numtrials_liveinterval_postinduc,livetrials_preinduc,livetrials_postinduc,...
            latencies_liveinterval_preinduc,latencies_liveinterval_postinduc,critlogical_liveinterval_preinduc,critlogical_liveinterval_postinduc,asynchtraces_liveinterval_preinduc,...
            asynchtraces_liveinterval_postinduc,asynchcriteria_liveinterval_preinduc,asynchcriteria_liveinterval_postinduc,rawtraces_liveinterval_preinduc,...
            rawtraces_liveinterval_postinduc,tooshorttomeasure,tooshorttomeasurenames]=find_time_windows(general_time_window_before, general_time_window_after,str2num(...
            preinduction_files{presentcase,8}),str2num(postinduction_files{presentcase,8}),general_info_files{presentcase,5},patchtitle, tooshorttomeasurenames,ensure_equal_lengths,...
            lookat_preinduc_ao_induc_ao_postinduc, interval_selection_method, pscs_fullepoch_preinduc, pscs_fullepoch_postinduc, numtrials_fullepoch_preinduc,...
            numtrials_fullepoch_postinduc, latencies_fullepoch_preinduc, latencies_fullepoch_postinduc, critlogical_fullepoch_preinduc, critlogical_fullepoch_postinduc, asynchtraces_fullepoch_preinduc, asynchtraces_fullepoch_postinduc,...
            asynchcriteria_fullepoch_preinduc, asynchcriteria_fullepoch_postinduc, rawtraces_fullepoch_preinduc, rawtraces_fullepoch_postinduc);
        
        %at this point loaded trial-by-trial variables are present as _fullepoch_preinduc, _fullepoch_postinduc, _liveinterval_preinduc and
        %_liveinterval_postinduc -- the 'livetrials' versions created here will cover whatever interval for whatever epoch(s) are to be analyzed by the analyses below
            pscs_livetrials=[pscs_liveinterval_preinduc pscs_liveinterval_postinduc];critlogical_livetrials=[critlogical_liveinterval_preinduc critlogical_liveinterval_postinduc];
            latencies_livetrials=[latencies_liveinterval_preinduc latencies_liveinterval_postinduc];asynchtraces_livetrials=[asynchtraces_liveinterval_preinduc asynchtraces_liveinterval_postinduc];
            asynchcriteria_livetrials=[asynchcriteria_liveinterval_preinduc,asynchcriteria_liveinterval_postinduc];rawtraces_livetrials=[rawtraces_liveinterval_preinduc rawtraces_liveinterval_postinduc];
            IPIs_livetrials=[pscs_livetrials(1:2,:)];
            
            time_axis=[-1*(numtrials_liveinterval_preinduc-1):numtrials_liveinterval_postinduc]*(general_info_files{presentcase,5}); %thus "trial 1" is the first postinduc trial
            time_axes{presentcase}=time_axis;
            pscmats{presentcase}=[pscs_liveinterval_preinduc pscs_liveinterval_postinduc];
            critmats{presentcase}=[critlogical_liveinterval_preinduc critlogical_liveinterval_postinduc];
        else
            tooshorttomeasure=0;
        end
                       
        %t-test and comparison of means and SEMs
        if ttest_psco_yesno && ~tooshorttomeasure && any(lookat_preinduc_ao_induc_ao_postinduc==[7])
             h=0;
             [h,p,ci,stats]=simpleteetester(pscs_liveinterval_preinduc(3:end,:),pscs_liveinterval_postinduc(3:end,:),ttestpsc_firstorall,writettestresulttodatabase_yesno,postinduction_files{presentcase,1},postinduction_files{presentcase,3});
             if h,significantlydifferentbefafnames=[significantlydifferentbefafnames,patchtitle];end
        end
        
        
        if analyze_and_plot_mean_responses_psc_yesno && ~tooshorttomeasure && any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7])
            mean_psc_and_error_matrix=analyze_and_plot_mean_responses(pscs_liveinterval_preinduc(3:end,:),pscs_liveinterval_postinduc(3:end,:),critlogical_liveinterval_preinduc,...
                critlogical_liveinterval_postinduc, casecolor, x_offset, do_rungs_plot_not_double_errobar_plot_psc, use_first_val_only_psc, figaddon_pscmeans);
            mean_psc_and_error_matrices=[mean_psc_and_error_matrices mean_psc_and_error_matrix];
        end       
        	
                       
        if  analyze_correlation_between_successive_pscs_yesno && any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7]) && any(interval_selection_method == [3 4])
            analyze_correlation_between_successive_values(IPIs_livetrials(1,:),pscs_livetrials(3:end,:),casecolor,psccorrelation_figaddon,'PSC')
        end
        if analyze_autocorrelation_psc_yesno && any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7]) && any(interval_selection_method == [3 4])
            analyze_autocorrelation(pscs_liveinterval_preinduc(3:end,:),use_first_val_only_pscautocorr,'PSC',general_info_files{presentcase,5},pscautocorr_figaddon,presentcase,casecolor,patchtitle)
        end
        
        if analyze_latencies_yesno && any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7])
            latency_summarizer(latencies_liveinterval_preinduc,latencies_liveinterval_postinduc,pscs_liveinterval_preinduc, pscs_liveinterval_postinduc, presentcase,latency_figaddon,...
                latencies_by_epoch, latencies_by_AP, latencies_by_contingency);
        end
        
        if careful_asynch_measurement_yesno && any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7])
           [asynch_count_by_trial,bgrd_asynch_count_by_trial,asynch_event_sizes_and_trials,bgrd_event_sizes_and_trials,preinduction_asynch_events,postinduction_asynch_events,...
               preinduction_bgrd_events,postinduction_bgrd_events]=careful_asynch_measurer(general_info_files{presentcase,4},...
               asynchcriterionmat,asynchtracemat,trialcountmat_fullrange,livetrials_preinduc,livetrials_postinduc,presentcase,general_info_files{presentcase,5});
           if ~isempty(asynch_event_data_holder),[asynch_event_data_holder,asynch_event_sizes_and_trials]=match_num_rows_by_addition(asynch_event_data_holder,asynch_event_sizes_and_trials);end
           asynch_event_data_holder=[asynch_event_data_holder asynch_event_sizes_and_trials];
           if ~isempty(bgrd_event_data_holder),[bgrd_event_data_holder,bgrd_event_sizes_and_trials]=match_num_rows_by_addition(bgrd_event_data_holder,bgrd_event_sizes_and_trials);end
           bgrd_event_data_holder=[bgrd_event_data_holder bgrd_event_sizes_and_trials];
           asynchmats{presentcase}=preinduction_asynch_events;
           bgrdasynchmats{presentcase}=preinduction_bgrd_events;
        elseif any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7])
            asynch_count_by_trial=repmat(nan,1,numtrials_liveinterval_preinduc+numtrials_liveinterval_postinduc);bgrd_asynch_count_by_trial=repmat(nan,1,numtrials_liveinterval_preinduc+numtrials_liveinterval_postinduc);
        end
        
        if careful_charge_calculator_yesno && any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7])
             if preind_presyn_pulse_duration~=postind_presyn_pulse_duration,disp(num2str([preind_presyn_pulse_duration postind_presyn_pulse_duration])),end
             for carefulcharge_interval_selection_method=carefulcharge_interval_selection_method_range
                 for first_waveform_only=first_waveform_only_yesno
                     for success_waveforms_only=success_waveforms_only_yesno
                         [charge_array_preinduc,charge_array_postinduc,meanpreindwave,meanpostindwave,special_waveform_matrix_preinduc,special_waveform_matrix_postinduc]=careful_charge_calculator(general_info_files{presentcase,3},...
                         general_info_files{presentcase,6},general_info_files{presentcase,5},general_info_files{presentcase,9},str2num(general_info_files{presentcase,10}),activate_local_offset_correction,general_time_window_before,... 
                         general_time_window_after,str2num(preinduction_files{presentcase,8}),str2num(postinduction_files{presentcase,8}),patchtitle,casecolor,presentcase,general_figure_position,...
                         carefulcharge_interval_selection_method,first_waveform_only,success_waveforms_only,max([preind_presyn_pulse_duration postind_presyn_pulse_duration]),pscs_fullepoch_preinduc(1:2,:),...
                         pscs_fullepoch_postinduc(1:2,:),rawtraces_fullepoch_preinduc,rawtraces_fullepoch_postinduc,critlogical_fullepoch_preinduc,critlogical_fullepoch_postinduc,...
                         numtrials_fullepoch_preinduc,numtrials_fullepoch_postinduc,ensure_equal_lengths,tooshorttomeasurenames);
                         if analyze_and_plot_mean_responses_charge_yesno && carefulcharge_interval_selection_method==1 && ~success_waveforms_only
                            %mean_charge_and_error_matrix=analyze_and_plot_mean_responses(livetrials_preinduc,livetrials_postinduc,critmats{presentcase},array_of_charges,casecolor,x_offset,70)    
                            %mean_charge_and_error_matrices=[mean_charge_and_error_matrices mean_charge_and_error_matrix];    
                         end
                         if ~first_waveform_only && carefulcharge_interval_selection_method==1
%                              [h_first,p_first,ci_first,stats_first]=ttest2(array_of_charges(1,livetrials_preinduc),array_of_charges(1,livetrials_postinduc));
%                              first_ratio=mean(array_of_charges(1,livetrials_postinduc))/mean(array_of_charges(1,livetrials_preinduc));
%                              [h_all,p_all,ci_all,stats_all]=ttest2(sum(array_of_charges(:,livetrials_preinduc)),sum(array_of_charges(:,livetrials_postinduc)));
%                              summed_ratio=mean(sum(array_of_charges(:,livetrials_postinduc)))/mean(sum(array_of_charges(:,livetrials_preinduc)));
%                              charge_p_value_holder=[charge_p_value_holder,[presentcase;h_first;p_first;first_ratio;h_all;p_all;summed_ratio]]
%                              
%                              [h_first_psc,p_first_psc,ci_first_psc,stats_first_psc]=ttest2(pscs_livetrials(3,livetrials_preinduc),pscs_livetrials(3,livetrials_postinduc));
%                              first_ratio_psc=mean(pscs_livetrials(3,livetrials_postinduc))/mean(pscs_livetrials(3,livetrials_preinduc));
%                              [h_all_psc,p_all_psc,ci_all_psc,stats_all_psc]=ttest2(sum(pscs_livetrials(3:end,livetrials_preinduc)),sum(pscs_livetrials(3:end,livetrials_postinduc)));
%                              summed_ratio_psc=mean(sum(pscs_livetrials(3:end,livetrials_postinduc)))/mean(sum(pscs_livetrials(3:end,livetrials_preinduc)));
%                              psc_p_value_holder=[psc_p_value_holder,[presentcase;h_first_psc;p_first_psc;first_ratio_psc;h_all_psc;p_all_psc;summed_ratio_psc]]
                         end
                     end
                 end
             end
             
            if analyze_release_dependence_yesno && any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7])
                %if carefulcharge_interval_selection_method ~=1, error('need charge array from all trials'),end
                RD_fisher_matrix2trig=analyze_release_dependence(critlogical_liveinterval_preinduc,critlogical_liveinterval_postinduc,pscs_liveinterval_preinduc,pscs_liveinterval_postinduc,...
                    charge_array_preinduc,charge_array_postinduc,special_waveform_matrix_preinduc,special_waveform_matrix_postinduc,presentcase,casecolor,patchtitle,RD_fisher_matrix2trig)
            end
            if analyze_history_dependence_yesno && any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7])
                analyze_history_dependence(pscs_livetrials,critlogical_livetrials,array_of_charges,trialcountmat_fullrange,livetrials_preinduc_specificwindow,livetrials_postinduc_specificwindow,[special_waveform_matrix_preinduc,...
                    special_waveform_matrix_postinduc],casecolor,presentcase,general_info_files{presentcase,6},patchtitle)           
            end
            if analyze_size_distributions_yesno && any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7])
                a=size(pscmat),b=size(critz),c=size(array_of_charges)
                analyze_size_distributions(pscmat_fullrange(3:end,:),array_of_charges,critz_fullrange,presentcase,[-1000:1000],{[livetrials_preinduc_specificwindow] [(livetrials_preinduc_specificwindow(end)+1):...
                    livetrials_postinduc_specificwindow(end)]});
            end
            if analyze_sd_over_time_yesno && any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7])
                sd_ratio_windows=[1 5 10 15 20 25];
                for current_sd_ratio_window=1:length(sd_ratio_windows)
                   [processed_sd,sd_ratio,sd_ratio_diff_time]=analyze_sd_over_time(time_axis,pscmat,critz,presentcase,general_info_files{presentcase,5},patchtitle,array_of_charges,...
                       use_psc_instead_of_charge_sdovertime,sd_ratio_windows,current_sd_ratio_window);
                   %sd_ratio_diff_times=[sd_ratio_diff_times sd_ratio_diff_time];
                   for sd_change_iteration=1:10
                       [numshuffles_neededfor_largerdiffthanorigdata,timemaxoriginaldif]=finding_confidence_for_timeofchangeinSTSD(sd_ratio(find(time_axis>=0)),...
                           round(sd_ratio_windows(current_sd_ratio_window)*inv(general_info_files{presentcase,5})),processed_sd(1,:),processed_sd(2,:));
                       sd_change_info_holder(presentcase,sd_change_iteration,current_sd_ratio_window)=numshuffles_neededfor_largerdiffthanorigdata;
                       difftimes=[difftimes,timemaxoriginaldif*general_info_files{presentcase,5}]
                   end
               end
            else
                processed_sd=nan;sd_ratio=nan;
            end
            if analyze_diary_yesno && any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7])
                [reliability,mean_potency]=analyze_diary(critz,general_info_files{presentcase,5},general_info_files{presentcase,6},pscmat,patchtitle,trialcountmat,casecolor,presentcase,asynch_count_by_trial,...
                    bgrd_asynch_count_by_trial,array_of_charges,general_figure_position,str2num(preinduction_files{presentcase,8}),str2num(postinduction_files{presentcase,8}),sd_ratio,processed_sd,use_psc_instead_of_charge_diary);
            end
            if analyze_realtime_yesno && any(lookat_preinduc_ao_induc_ao_postinduc==[2 7])
                individual_induction_traces=analyze_realtime(induction_files{presentcase,1},(1000/induction_files{presentcase,2}),induction_files{presentcase,3},casecolor,'-',presentcase,...
                    meanpreindwave,meanpostindwave,general_figure_position,raw_induction_trace,induction_peaks,induction_pulse_duration,induction_sampling_rate);
                %summed_slopes_each_induction=[summed_slopes_each_induction sum(slopes_this_induction)];
                %mean_induction_traces{presentcase}=mean_induction_trace;
                individual_induction_traces_all_expts{presentcase}=individual_induction_traces;
                %slopes_each_induction{presentcase}=slopes_this_induction;
            end
            if  analyze_correlation_between_successive_charges_yesno && any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7]) && interval_selection_method == 3
                analyze_correlation_between_successive_values([pscs_liveinterval_preinduc(1,:) pscs_liveinterval_postinduc(1,:)],[...
                    ],casecolor,chargecorrelation_figaddon,'charge')
            end
            if analyze_autocorrelation_charge_yesno && any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7]) && interval_selection_method == 3
                analyze_autocorrelation(pscmat_fullrange(3,1:trialcountmat(1)),casecolor,patchtitle,presentcase,general_info_files{presentcase,5})
            end
             
        elseif any(lookat_preinduc_ao_induc_ao_postinduc==[1 3 7])
            %array_of_charges=array_of_charges_cell_array{presentcase};
            array_of_charges=repmat(nan,1,length(livetrials_preinduc)+length(livetrials_postinduc));
            meanpreindwave=nan;meanpostindwave=nan;special_waveform_matrix_preinduc=nan;special_waveform_matrix_postinduc=nan;
        end
        %array_of_charges_fullrange_cell_array{presentcase}=array_of_charges;%rawmat_cell_array{presentcase}=rawmat_specificcontinuouswindowrange;critz_cell_array{presentcase}=critz_specificcontinuouswindowrange;
        %pscmat_cell_array{presentcase}=pscmat_specificcontinuouswindowrange; 
        %special_waveforms_cell_array{1,presentcase}=special_waveform_matrix_preinduc;
        %special_waveforms_cell_array{2,presentcase}=special_waveform_matrix_postinduc;
           
        if any(changepoints_to_find)   
            cp_output_thistrial=[general_info_files{presentcase,4};presentcase];
            cp_output_thistrial=changepoint_coordinator(changepoints_to_find, cp_output_thistrial, num_bootstrap_runs, num_permutations, cp_ID_method, bootstrap_style,...
                general_info_files{presentcase,6},trialcountmat(1), time_axis, reliability,mean_potency,pscmat,critz,array_of_charges, processed_sd, sd_ratio, asynch_count_by_trial,...
                bgrd_asynch_count_by_trial)
            cp_output_mat=[cp_output_mat cp_output_thistrial]
            if save_changepoint_matrix
                clkchk=clock; save('clockcheck','clkchk','-ascii','-tabs','-double'); save('cp_output_mat_by_trial','cp_output_mat','-ascii','-tabs','-double');
            end
        end
    else
        disp('No matching cases found')
    end
    if close_figs_after_each_loop,close all,end
end

warning on MATLAB:divideByZero
toc

if batch_exporting
    batch_export_figures
end

%storing/displaying the record of what was queried & record of what was returned
if instant_archiving
    %case_numbers_and_trialcountmats=[case_numbers_and_trialcountmats [presentcase;livetrials_preinduc_specificwindow(end);trialcountmat_fullrange(1);trialcountmat_fullrange(2);trialcountmat_specificcontinuouswindowrange(1);trialcountmat_specificcontinuouswindowrange(2);trialcountmat_specificdiscretewindowrange(1);trialcountmat_specificdiscretewindowrange(2)]];
	%fortherecord=strcat(selection_clauses{1},preinduction_files{:,1},'___',selection_clauses{2},induction_files{:,1},'___',selection_clauses{3},postinduction_files{:,1});
    %fortherecord=strcat(preinduction_files{:,1},'___',induction_files{:,1},'___',postinduction_files{:,1});
    dd=''; 
    for ii=1:size(preinduction_files,1), aa=preinduction_files{ii,1}; bb=induction_files{ii,1}; cc=postinduction_files{ii,1};  dd=strcat(dd,aa,'__',bb,'__',cc,'stop');  end
end