function loadRepeatRecord_byChannel(fid_prefix)
%function loadRepeatRecord_byChannel(fid_prefix,badchannels)

channel = [];
time = [];
threshold = [];
waveform = [];
j = 1;

% if nargin<2
%     badchannels = [1 8 33 57 64]; %neurorighter analog & ground channels mapping
% end

time = [];
waveform = [];
threshold = [];

fpath = ['C:\Users\USER\Documents\mingfaifong\' fid_prefix '\spkfiles\'];
spath = ['C:\Users\USER\Documents\mingfaifong\' fid_prefix '\bychannel\']; mkdir(spath);
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

for k = 1:64
    save([spath fid_prefix '_chan' sprintf('%02d',k) '.mat'],'time','threshold','waveform');
end

for i = 1:length(fidlist)

    fid = fidlist{i};

    % --- determine time at beginning of file based on location of dashes in filename  --- %
    dashloc = strfind(fid,'-');
    %uscloc = strfind(fid,'_');
    T = [str2double(fid(dashloc(length(dashloc)-5)+1:dashloc(length(dashloc)-4)-1)),... %year
        str2double(fid(dashloc(length(dashloc)-4)+1:dashloc(length(dashloc)-3)-1)),... %month
        str2double(fid(dashloc(length(dashloc)-3)+1:dashloc(length(dashloc)-2)-1)),... %day
        str2double(fid(dashloc(length(dashloc)-2)+1:dashloc(length(dashloc)-1)-1)),... %hour
        str2double(fid(dashloc(length(dashloc)-1)+1:dashloc(length(dashloc))-1)),... %minute
        str2double(fid(dashloc(length(dashloc))+1:length(fid)-4))]; %second

    % --- set T0 as time of first file --- %
    if i == 1
        T0 = T;
    end

    % --- load spike file and adjust time --- %
    if strfind(fid,'.spk')
        x = loadspike([fpath fid]);
    elseif strfind(fid,'.mat')
        x = load([fpath fid]);
    else
        disp([fid ' was not loaded.']);
    end
    
    starttime = etime(T,T0);
    x.time = x.time+starttime; %increments all times up by prior recordings opened so far
    
    for j = 1:64
        %if find(badchannels==j)
        %    break
        %else
            tmp.time = x.time(x.channel==j);
            tmp.threshold = x.threshold(x.channel==j);
            tmp.waveform = x.waveform(:,x.channel==j);
%             if isempty(tmp.time)
%                 break;
%             end
            load([spath fid_prefix '_chan' sprintf('%02d',j) '.mat']);
            time = [time; tmp.time];
            threshold = [threshold; tmp.threshold];
            waveform = [waveform, tmp.waveform];
            if i == length(fidlist)
                %keyboard;
                channel = j*ones(size(time));
                save([spath fid_prefix '_chan' sprintf('%02d',j) '.mat'],'time','threshold','waveform','channel');
                clear time threshold waveform channel
            else
                save([spath fid_prefix '_chan' sprintf('%02d',j) '.mat'],'time','threshold','waveform');
                clear time threshold waveform
            end
        %end
    end
end
