fpath = 'C:\Users\USER\Documents\mingfaifong_tmp\2011.02.28 Chronic AMPAR Blockade & Recovery (Hamburger)\hamburger360 (NR)\';
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

spktimes = [];
channels = [];
cleanspk = [];

% this is for plotting AVERAGE WAVEFORM for all recordings
% and for creating an array of spike times and channels for the entire
% session
for i = 1:length(fidlist)
    fid = fidlist{i};
    if strfind(fid,'.SS')
        load([fpath fid],'-mat');
    else
        break
    end
    SS.RemoveUnit();
    
    spktimes = [spktimes; SS.time];
    channels = [channels; SS.channel];
    cleanspk = [cleanspk; SS.clean];
    
    close all;
    SS.PlotAvgWaveform;
    export_fig(gcf,['AvgWaveform_' fid],'-nocrop','-png');
    saveas(gcf,['AvgWaveform_' fid '.fig'],'fig');
    
    %keyboard;
    
    SS.Save;
    clearvars -except spktimes channels cleanspk i fidlist fpath
end

save([fpath fid],'spktimes','channels','cleanspk');

clear all;

spktimes = [];
channels = [];
cleanspk = [];

load max_predrug_time;

fidlist = {'frodo364_predrug_0to150min_SS',...
    'frodo364_predrug_150to300min_SS',...
    'frodo364_predrug_300to450min_SS',...
    'frodo364_predrug_450to566min_SS'};

for i = 1:length(fidlist)
    fid = fidlist{i};
    load([fid '.SS'],'-mat');
    SS.RemoveUnit();
    
    spktimes = [spktimes; SS.time-max_predrug_time];
        % NOTE: when processing, still need to subtract time taken to add drug
    channels = [channels; SS.channel];
    cleanspk = [cleanspk; SS.clean];
    
    close all;
    SS.PlotAvgWaveform;
    fid_savename = fid(1:length(fid)-3);
    export_fig(gcf,['AvgWaveform_' fid_savename],'-nocrop','-png');
    saveas(gcf,['AvgWaveform_' fid_savename '.fig'],'fig');
    
    SS.Save;
    clearvars -except spktimes channels cleanspk i fidlist max_predrug_time
    
end

save('frodo364_spktimes_predrug.mat','spktimes','channels','cleanspk');