function stsd_grid_plotter_raw_flat_hy(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,...
           show_colorcode_bar,extendedrawaxis_followinglastpulse)
%called by convert_stored_matrix

raw_start_point=200;   %in samples -- this depends totally on what is set in patchfile_analysis
max_length_raw=raw_axis(2)*samples_per_ms;   
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;
clrmp=colormap(jet(size(plain_rawmatrix,2)));
panelwidth=.220;
panelheight=inv(length(hy))-.01;

failure_check_region=[250:450];     %for using threshold-based, rather than template match-based, event detection
failure_threshold=-4;
chargebased_failure_thresh=nan; %this has to be set on the basis of how the charge histograms look -- where is the failure threshold?

cluster_fitted_curves=0;
if cluster_fitted_curves
    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
end

figure
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
        floor_of_plot=(length(hy)-k)/length(hy);if floor_of_plot==0,floor_of_plot=.005;end
        
        trialswithcorrect_IPI_thisepoch=trials_that_were_during_this_epoch(find(plain_rawmatrix(2,trials_that_were_during_this_epoch)==hy(k))); %second row is the relevant one for flat_hy expts
                
        if excluding_failure_waveforms
            matches_o=trialswithcorrect_IPI_thisepoch(find(critz(1,trialswithcorrect_IPI_thisepoch)))
            matches_a=trialswithcorrect_IPI_thisepoch(find(critz(2,trialswithcorrect_IPI_thisepoch)))
            matches_b=trialswithcorrect_IPI_thisepoch(find(critz(3,trialswithcorrect_IPI_thisepoch)))
            matches_c=trialswithcorrect_IPI_thisepoch(find(critz(4,trialswithcorrect_IPI_thisepoch)))
        else
            matches_o=trialswithcorrect_IPI_thisepoch;
            matches_a=trialswithcorrect_IPI_thisepoch;
            matches_b=trialswithcorrect_IPI_thisepoch;
            matches_c=trialswithcorrect_IPI_thisepoch;
        end
        
        subplot('position',[0.1 floor_of_plot panelwidth panelheight])
        if k==1,ylabel('50 ms, 50 ms, 50 ms trials','fontweight','bold','fontsize',20), else ylabel('50 ms, 400 ms, 50 ms trials','fontweight','bold','fontsize',20),end
        relevant_raw_rows=[raw_start_point:(raw_start_point+max_length_raw-1)];
        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)
        axis(raw_axis)
        
        subplot('position',[0.325 floor_of_plot panelwidth panelheight])
        relevant_raw_rows_partdeux=relevant_raw_rows+(50*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)
        axis(raw_axis)
             
        subplot('position',[0.55 floor_of_plot panelwidth panelheight])
        relevant_raw_rows_parttrois=relevant_raw_rows_partdeux+(hy(k)*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)
        axis(raw_axis)
            
        subplot('position',[0.775 floor_of_plot panelwidth panelheight])
        relevant_raw_rows_partquatre=relevant_raw_rows_parttrois+(50*samples_per_ms);x_vals_quatre=x_vals;quatraxis=raw_axis;
        if extendedrawaxis_followinglastpulse(1)
            relevant_raw_rows_partquatre=[relevant_raw_rows_partquatre relevant_raw_rows_partquatre(end)+1:relevant_raw_rows_partquatre(end)+(extendedrawaxis_followinglastpulse(2)*samples_per_ms)];
            x_vals_quatre=[inv(samples_per_ms):inv(samples_per_ms):(length(relevant_raw_rows_partquatre)/samples_per_ms)];
            quatraxis(2)=length(relevant_raw_rows_partquatre)/samples_per_ms;
        end
        stsd_grid_plotter_raw3innerportion(relevant_raw_rows_partquatre,matches_c,plain_rawmatrix,meanmode_raw,x_vals_quatre,parameter_colors,epoch,raw_include_SD,...
                indivmode_raw,raw_comparison_format,epochal_colormaps,raw_axis,clrmp,0)
        axis(quatraxis)        
            
        if k==1,
            insetwidth=.05; insetheight=.3; rawxspan=raw_axis(2)-raw_axis(1);rawyspan=raw_axis(4)-raw_axis(3);
            subplot('position',[.025 .6 insetwidth insetheight]),axis([0 1 0 1]),axis off
            fifthrawxspan=rawxspan/5; fifthrawxdistance=panelwidth/5;
            rectangle('position',[0 0 fifthrawxdistance/insetwidth .02],'facecolor','k','edgecolor','k')
            text(.3, -.05, strcat(num2str(fifthrawxspan),'ms'),'fontweight','bold','fontsize',20)
            hold on
            fifthrawyspan=rawyspan/5; fifthrawydistance=panelheight/5;
            rectangle('position',[0 0 .1 fifthrawydistance/insetheight],'facecolor','k','edgecolor','k')
            text(-.15, .15, strcat(num2str(fifthrawyspan),'pA'),'rotation',90,'fontweight','bold','fontsize',20)
        end
            
        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

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',[.025 .05 .05 .5]), axis off
    text(0,0,conjoined_filename,'rotation',90,'fontweight','bold')
end     

if show_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
        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
end