function batch_plotAvgWaveform(fid_prefix,fidlist)

close all;

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

for j = 1:64

    for i = 1:length(fidlist)
        fid = fidlist{i};
        load([fpath fid],'-mat');

        if isempty(SS.unit)
            disp(['no units detected on Ch' num2str(j)]);
        else
            close all;
            PlotAvgWaveform_mingfaifong(SS);
            export_fig(gcf,[spath fid_prefix '_avgwaveform_' fid(length(fid_prefix)+2:length(fid)-3)],'-nocrop','-png');
            print('-depsc',[spath fid_prefix '_avgwaveform_' fid(length(fid_prefix)+2:length(fid)-3) '.eps']);
            saveas(gcf,[spath fid_prefix '_avgwaveform_' fid(length(fid_prefix)+2:length(fid)-3) '.fig'],'fig');
        end

        clearvars -except i fidlist fpath fid_prefix spath

    end
end
clear all; close all;