function stsd_grid_plotter_raw3(plain_rawmatrix,azaz_record,conjoined_filename,NLize_raw,hy,raw_axis,meanmode_raw,raw_include_SD,indivmode_raw,raw_movie_format,...
           raw_comparison_format,epoch_dividers,parameter_colors,samples_per_ms,epochal_colormaps,plain_extractedmatrix,excluding_failure_waveforms,fit_array,analyze_fitted_curves)
%proofread april 10, 2006

raw_start_point=200;   %in samples -- this depends totally on what is set in raw_reader_core for number of raw samples to keep
failure_check_region=[250:450];
failure_threshold=-4
max_length_raw=raw_axis(2)*samples_per_ms;   
relevant_raw_rows=[raw_start_point:(raw_start_point+max_length_raw-1)];
number_of_trials=size(plain_rawmatrix,2)
x_vals=[inv(samples_per_ms):inv(samples_per_ms):(max_length_raw/samples_per_ms)];
floor_of_plot=0;
%chargebased_failure_thresh=nan; %this has to be set on the basis of how the charge histograms look -- where is the failure threshold?
clrmp=colormap(jet(size(plain_rawmatrix,2)));

cluster_fitted_curves=0
if cluster_fitted_curves
    %fitzz=[fit_array(1:4,:)'; fit_array(10:13,:)'; fit_array(19:22,:)'];
    template_samples=[];
    for trial=1:size(fit_array,2)
        for psc=1:3
            correct_rows=((psc-1)*9)+[1:4]';
            template_vals=fit_array(correct_rows,trial);
            template=lsqfit_template_generator([template_vals' 0 0],[1:300]);
            template_sampling=template(1:20:end);
            figure(1),plot([1:20:length(template)],template_sampling,'k'),hold on,plot(template,'r')
            template_samples=[template_samples;template_sampling];
        end
	end
    idx=kmeans(template_samples,3)
    fitss=[];while ~isempty(idx),fitss=[fitss idx(1:3)];idx(1:3)=[];end, fitss
%     sss=size(fit_array,2);
%     fitss=[idx(1:sss)';idx(sss+1:2*sss)';idx(1+(2*sss):end)']
end

figure%(23)

for j=1:2   %raw_o is when j==1, raw_a is when j==2 (raw_b is handled under j==2)
    if j==1
        if ~isempty(epoch_dividers),trials_that_were_during_this_epoch=[1:epoch_dividers(1)-.5];else trials_that_were_during_this_epoch=1:number_of_trials;end
		for epoch=1:length(epoch_dividers)+1
            matches=trials_that_were_during_this_epoch;
            %
            if excluding_failure_waveforms,matches=find_failures_for_raw_plot(matches,plain_rawmatrix,failure_check_region,failure_threshold);end
            if isempty(matches),break,end
            %subplot('position',[0.1 .5-(.5*inv(length(hy))) .3 inv(length(hy))])
%             stsd_grid_plotter_raw3innerportion(relevant_raw_rows,matches,plain_rawmatrix,meanmode_raw,x_vals,parameter_colors,epoch,raw_include_SD,...
%                 indivmode_raw,raw_comparison_format,epochal_colormaps,raw_axis,clrmp,1)
            %--------------------
            if analyze_fitted_curves,applicable_fit_rows=[1:6];fitted_curve_plotter(matches,fit_array,applicable_fit_rows,clrmp),end
%             if cluster_fitted_curves, for ijk=1:length(matches),text(20+ijk,0,num2str(fitss(1,matches(ijk))),'fontsize',20,'fontweight','bold','color',...
%                             clrmp(matches(ijk),:)),hold on,end,end
            if length(epoch_dividers)>epoch,trials_that_were_during_this_epoch=[epoch_dividers(epoch)+.5 :epoch_dividers(epoch+1)-.5];
			elseif length(epoch_dividers)==epoch,trials_that_were_during_this_epoch=[epoch_dividers(epoch)+.5:number_of_trials];% mem=number_of_trials,disp('babbyshka')
            end
		end
    elseif j==2  
        for k=1:length(hy)
            if ~isempty(epoch_dividers),trials_that_were_during_this_epoch=[1:epoch_dividers(1)-.5];else trials_that_were_during_this_epoch=1:number_of_trials;end
		    for epoch=1:length(epoch_dividers)+1
                matches_a=trials_that_were_during_this_epoch(find(plain_rawmatrix(1,trials_that_were_during_this_epoch)==hy(k)));
                if isempty(matches_a),break,end
                %
                if excluding_failure_waveforms
                    failure_check_region_a=failure_check_region+(samples_per_ms*hy(k));       %failure_check_region; for checking the PSC_o response
                    matches_a=find_failures_for_raw_plot(matches_a,plain_rawmatrix,failure_check_region_a,failure_threshold);
                end
                if isempty(matches_a),break,end
                floor_of_plot=(length(hy)-k)/length(hy);
                subplot('position',[0.1 floor_of_plot .3 inv(length(hy))])
                matches_o=trials_that_were_during_this_epoch(find(plain_rawmatrix(1,trials_that_were_during_this_epoch)==hy(k)));
                stsd_grid_plotter_raw3innerportion(relevant_raw_rows,matches_o,plain_rawmatrix,meanmode_raw,x_vals,parameter_colors,epoch,raw_include_SD,...
                    indivmode_raw,raw_comparison_format,epochal_colormaps,raw_axis,clrmp,0)
                subplot('position',[0.4 floor_of_plot .3 inv(length(hy))])
                relevant_raw_rows_partdeux=relevant_raw_rows+(hy(k)*samples_per_ms);
                stsd_grid_plotter_raw3innerportion(relevant_raw_rows_partdeux,matches_a,plain_rawmatrix,meanmode_raw,x_vals,parameter_colors,epoch,raw_include_SD,...
                    indivmode_raw,raw_comparison_format,epochal_colormaps,raw_axis,clrmp,0)
                %--------------------
                if analyze_fitted_curves,applicable_fit_rows=[10:15];fitted_curve_plotter(matches_a,fit_array,applicable_fit_rows,clrmp),end
                if cluster_fitted_curves, for ijk=1:length(matches_a),text(20+ijk,0,num2str(fitss(2,matches_a(ijk))),'fontsize',30,'fontweight','bold','color',...
                            clrmp(matches_a(ijk),:)),hold on,end,end
                
                matches_b=trials_that_were_during_this_epoch(find(plain_rawmatrix(1,trials_that_were_during_this_epoch)==hy(k)));
                if isempty(matches_b),break,end
                %
                if excluding_failure_waveforms  
                    failure_check_region_b=failure_check_region_a+(50*samples_per_ms);      %failure_check_region+(samples_per_ms*hy(k)); for checking the PSC_a response
                    matches_b=find_failures_for_raw_plot(matches_b,plain_rawmatrix,failure_check_region_b,failure_threshold);
		        end
                if isempty(matches_b),break,end
                floor_of_plot=(length(hy)-k)/length(hy);
                subplot('position',[0.7 floor_of_plot .3 inv(length(hy))])
                relevant_raw_rows_parttrois=relevant_raw_rows_partdeux+(50*samples_per_ms);
                stsd_grid_plotter_raw3innerportion(relevant_raw_rows_parttrois,matches_b,plain_rawmatrix,meanmode_raw,x_vals,parameter_colors,epoch,raw_include_SD,...
                        indivmode_raw,raw_comparison_format,epochal_colormaps,raw_axis,clrmp,0)
                %--------------------
                if analyze_fitted_curves,applicable_fit_rows=[19:24];fitted_curve_plotter(matches_b,fit_array,applicable_fit_rows,clrmp),end
                if cluster_fitted_curves, for ijk=1:length(matches_b),text(20+ijk,0,num2str(fitss(3,matches_b(ijk))),'fontsize',30,'fontweight','bold','color',...
                            clrmp(matches_b(ijk),:)),hold on,end,end
            
                if length(epoch_dividers)>epoch,trials_that_were_during_this_epoch=[epoch_dividers(epoch)+.5 :epoch_dividers(epoch+1)-.5]
				elseif length(epoch_dividers)==epoch,trials_that_were_during_this_epoch=[epoch_dividers(epoch)+.5:number_of_trials];% mem=number_of_trials,disp('babbyshka')
                end 
            end
        end
    end
end
% 
% %if epoch==length(epoch_dividers)+1
%     %set(gcf,'PaperOrientation','landscape'),set(gcf,'PaperPosition',[0.5 0.5 10 7.5])
%     %set(gcf,'position',[ 1          31        1600        1094])
%     subplot('position',[0.1 .5-(.5*inv(length(hy))) .3 inv(length(hy))])
%     %set(gca,'xticklabel',get(gca,'xticklabel'),'fontsize',10,'fontweight','bold','fontname','book antiqua')
%     xlabel('Time (ms)', 'fontsize',20,'fontname','book antiqua','fontweight','bold')
%     ylabel('I_m_e_m (pA)', 'fontsize',20,'fontname','book antiqua','fontweight','bold')
%     %subplot(20,5,11),text(0,0,conjoined_filename,'fontweight','bold','fontsize',6,'fontname','book antiqua'),axis off
% %end     
% 
% %colorcode bar
% subplot(25,3,70)
% if meanmode_raw | raw_comparison_format
%     title('{\itEpoch #} ','fontsize',15,'fontname','book antiqua','fontweight','bold'),hold on
%     holdr=0;
%     for ep=1:length(epoch_dividers)+1
%         rectangle('position',[holdr 0 1 1],'facecolor',parameter_colors{(mod(ep,length(parameter_colors)))}),hold on
%         text(holdr+.4,0.25,num2str(ep),'fontsize',15,'fontweight','bold','color','w')
%         holdr=holdr+1;
%     end
%     axis off
% else
%     colormap jet
%     %zzzz=number_of_trials, figure(33),imagesc([1:number_of_trials]), figure(2)
% 	imagesc([1:number_of_trials])
%     set(gca,'ytick',[])
% 	%set(gca,'xTickLabel',get(gca,'xticklabel'),'fontsize',12,'fontname','book antiqua','fontweight','demi')
% 	title('{\itTrial #} ','fontsize',15,'fontname','book antiqua','fontweight','bold'),hold on
% 	for m=1:length(epoch_dividers),plot([epoch_dividers(m) epoch_dividers(m)],[0 2],'w','linewidth',2),end
% 	%text(1,floor(.1*size(plain_rawmatrix,2)),'{\itTrial} \rightarrow','fontsize',15, 'fontname', 'Book Antiqua','fontweight','bold','color','w')
% end