% d = abs(x_11to12hr_unit2 - x_10to11hr_unit2);
% sq_d = (x_11to12hr_unit2 - x_10to11hr_unit2).^2;
%
% test on Ch1, unit1

fid_prefix = 'cinderella360';
fpath = ['C:\Users\USER\Documents\mingfaifong\' fid_prefix '\SSfiles\'];
spath = ['C:\Users\USER\Documents\mingfaifong\' fid_prefix '\figures\'];  mkdir(spath);


if nargin<2
    fpath_info = dir(fpath);      %get info about contents of directory
    isfile = ~[fpath_info.isdir]; %get index for files only
    fidlist = {fpath_info(isfile).name}; %create list of files
end

% this is for plotting AVERAGE WAVEFORM for all recordings
% and for creating an array of spike times and channels for the entire
% session

n = 0;
fullscreen = get(0,'ScreenSize');

for k = 1:64
    n = 0;
close all;
    figure('Position',[0 -50 fullscreen(3) fullscreen(4)])

for i = 1:length(fidlist)
    fid = fidlist{i};
    load([fpath fid],'-mat');
    
    for j = 1:max(SS.unit)
        if SS.channel(SS.unit==j)==k
        
        n = n+1
        
        if n <= 100
            subplot(10,10,n);
        elseif n < 200
            if n == 101
            export_fig(gcf,[spath fid_prefix '_avgwaveform_Ch' num2str(k) '_1'],'-nocrop','-png');
                close all; figure('Position',[0 -50 fullscreen(3) fullscreen(4)])
            end
            subplot(10,10,n-100);
        elseif n < 300
            if n == 201
            export_fig(gcf,[spath fid_prefix '_avgwaveform_Ch' num2str(k) '_2'],'-nocrop','-png');
                close all; figure('Position',[0 -50 fullscreen(3) fullscreen(4)])
            end
            subplot(10,10,n-200);
        elseif n < 400
            if n == 301
            export_fig(gcf,[spath fid_prefix '_avgwaveform_Ch' num2str(k) '_3'],'-nocrop','-png');
                close all; figure('Position',[0 -50 fullscreen(3) fullscreen(4)])
            end
            subplot(10,10,n-300);
        elseif n < 500
            if n == 401
            export_fig(gcf,[spath fid_prefix '_avgwaveform_Ch' num2str(k) '_4'],'-nocrop','-png');
                close all; figure('Position',[0 -50 fullscreen(3) fullscreen(4)])
            end
            subplot(10,10,n-400);
        else
            'dude, seriously?'
        end

        
%         confplot(1000/SS.fs.*(1:size(SS.waveform(:,SS.unit==j),1)),...
%             mean(SS.waveform(:,SS.unit==j),2),...
%             std(SS.waveform(:,SS.unit==j)'),...
%             std(SS.waveform(:,SS.unit==j)'),...
%             'LineWidth',2.5,'FontSize',9);
        
         plot(mean(SS.waveform(:,SS.unit==j),2),'LineWidth',2.5);
         set(gca,'FontSize',8)        
%         confplot(1000/SS.fs.*(1:length(SS.avgwaveform.avg(:,it))),...
%         SS.avgwaveform.avg(:,it),...
%         SS.avgwaveform.std(:,it),...
%         SS.avgwaveform.std(:,it),...
%         'LineWidth',2,'color', col);
%     set(gca,'FontSize',9);

    x1 = strfind(fid,'_');
    x2 = strfind(fid,'.');
    title([fid(x1+1:x2-1) ' Unit' num2str(j)],'FontSize',10)
    axis tight
    %ylim([min_amp max_amp])
    ylim([-60 60]);
    grid on;
        end
    end

    end
    
%    avgwaveform(:,i) = mean(SS.waveform(:,SS.unit==1),2);
%     if i > 1
%         sum_sq_d(1,i) = sum((avgwaveform(:,i-1) - avgwaveform(:,i)).^2);
%     end
%    keyboard
    export_fig(gcf,[spath fid_prefix '_avgwaveform_Ch' num2str(k)],'-nocrop','-png');
end


