function loadRepeatRecord_chunkAlt(fid_prefix,chunksize)

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

if nargin<2
    chunksize = 1; %in hours
end

fpath = ['C:\Users\USER\Documents\mingfaifong\' fid_prefix '\spkfiles\'];
spath = ['C:\Users\USER\Documents\mingfaifong\' fid_prefix '\matfiles\']; 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
fdate = {fpath_info(isfile).date}; %create list of date modified

for i = 1:length(fidlist)
    clearvars -except i fidlist T0 fpath chunksize j channel time threshold waveform spath fid_prefix fdate
    fid = fidlist{i};
    %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
    
    T = datevec(addtodate(datenum(fdate{i}),-max(x.time),'second'))
    
    if i == 1
        T0 = T; %set T0 as time of first file
    end

    starttime = etime(T,T0)
    x.time = x.time+starttime;

    [maxval maxvalind] = max(x.time);
    minval = min(x.time);
    
    %keyboard;
    
    %when the earliest time in the next file that has been opened has a
    %value greater than the duration of the current epoch, the current
    %times/channels/etc get saved to disc
    if minval > chunksize*j*3600
        %fid_prefix = fid(1:dashloc(length(dashloc)-5)-1);
        save([spath fid_prefix '_' num2str(chunksize*(j-1)) 'to' num2str(chunksize*j) 'hr' '.mat'],'channel','time','threshold','waveform');
        channel = [];
        time = [];
        threshold = [];
        waveform = [];
        while minval > chunksize*j*3600
            j = j+1;
        end
    elseif minval <=chunksize*j*3600 && maxval < chunksize*j*3600
        channel = [channel; x.channel];
        time = [time; x.time];
        threshold = [threshold; x.threshold];
        waveform = [waveform, x.waveform];
    end
    
    
    %when the latest time in the next file that has been opened has a value
    %greater than the duration of the current epoch, the times/channels/etc
    %values are appended to the current data, and only the portion up to
    %the end of the epoch is saved to disc.  the remains in the
    %time/channels/etc variables.
    while maxval >= chunksize*j*3600
        
        highind = find(x.time >= chunksize*j*3600);
        channel = [channel; x.channel(1:highind(1)-1)];
        time = [time; x.time(1:highind(1)-1)];
        threshold = [threshold; x.threshold(1:highind(1)-1)];
        waveform = [waveform, x.waveform(:,1:highind(1)-1)];
        save([spath fid_prefix '_' num2str(chunksize*(j-1)) 'to' num2str(chunksize*j) 'hr' '.mat'],'channel','time','threshold','waveform');
        clear channel time threshold waveform
        
        j = j+1;
        [maxval maxvalind] = max(x.time(highind));
        
        if maxval >= chunksize*j*3600
            x.channel = x.channel(highind);
            x.time = x.time(highind);
            x.waveform = x.waveform(:,highind);
            x.threshold = x.threshold(highind);
            channel = [];
            time = [];
            waveform = [];
            threshold = [];
        else
            channel = x.channel(highind);
            time = x.time(highind);
            waveform = x.waveform(:,highind);
            threshold = x.threshold(highind);
% 
%             channel = [channel; x.channel];
%             time = [time; x.time];
%             threshold = [threshold; x.threshold];
%             waveform = [waveform, x.waveform];
        end
        
    end
    
end

save([spath fid_prefix '_' num2str(chunksize*(j-1)) 'to' num2str(ceil(max(time)/3600)) 'hr' '.mat'],'channel','time','threshold','waveform');