function loadspk_chunk(fid_prefix,chunksize)

channel = [];
time = [];
threshold = [];
waveform = [];
h = 1;
tmp.time = [];
tmp.channel = [];
tmp.waveform = [];
tmp.unit = [];
tmp.threshold = [];

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

for i = 1:length(fidlist)
    clearvars -except i fidlist T0 fpath chunksize j channel time threshold waveform spath fid_prefix tmp h
    
    % get name of spike file
    fid = fidlist{i};
    
    % finding fid name based on location of underscores
    uscloc = strfind(fid,'_');
    if length(uscloc)==1
        fidsave = fid;
    else
        fidsave = fid(1:uscloc(2)-1);
    end

    % get start time for associated fid
    %load([fid_prefix '\' fid(1:length(fid)-4),'_starttime.mat']);
    % get start time from meta data
    mspk = mloadspk([fpath fid], 'meta');
    T = [];
    starttime = mspk.meta.date;
    rest = starttime;
    while ~isempty(rest)
        [token rest] = strtok(rest,'-');
        T = [T str2double(token)];
    end
    
    %set T0 as time of first file
    if i == 1
        T0 = T;
    end
    
    %get time of final spk in file
    lastspk = loadspk([fpath fid],'last');

%     for j = 1:ceil(lastspk/3600)
%         
%         if j == ceil(lastspk/3600)
%             %loads only remaining spikes in file and writes to tmp variable
%             tmp = loadspk([fpath fid],[(j-1)*chunksize*3600 lastspk]);
%             tmp.duration = [0 lastspk-(j-1)*chunksize*3600];
%         
%         else
%             %loads 1 chunksize of data from spkfile            
%             loadspk([fpath fid],[(j-1)*chunksize*3600-tmp.duration(1) j*chunksize*3600]-tmp.duration(2));
%             
%             %increments time appropriately
%             starttime = etime(T,T0);
%             time = time+starttime;
%             time = [tmp.time; time];
%             channel = [tmp.channel; channel];
%             waveform = [tmp.waveform waveform];
%             unit = [tmp.unit; unit];
%             threshold = [tmp.threshold; threshold];
%             
%             tmp.time = []; tmp.waveform = []; tmp.unit = []; tmp.channel = []; tmp.threshold = [];
%             tmp.duration(1) = tmp.duration(2);
%             
% 
%             %increments time appropriately
%             save([spath fidsave '_' num2str(chunksize*(j-1)) 'to' num2str(ceil(max(time)/3600)) 'hr' '.mat'],'channel','time','threshold','waveform','unit');                
%         end
%     end
    
    starttime = etime(T,T0);
    for j = 1:ceil(lastspk/3600)
        
        struct = mloadspk([fpath fid], [((h-1)*3600)-starttime (h*3600)-starttime]);
        if j == ceil(lastspk/3600)
            tmp.time = struct.time+starttime;
            tmp.channel = struct.channel;
            tmp.waveform = struct.waveform;
            tmp.unit = struct.unit;
            tmp.threshold = struct.threshold;
        else
            time = [tmp.time; struct.time+starttime];
            channel = [tmp.channel; struct.channel];
            waveform = [tmp.waveform struct.waveform];
            unit = [tmp.unit; struct.unit];
            threshold = [tmp.threshold; struct.threshold];
            tmp.time = [];
            tmp.channel = [];
            tmp.waveform = [];
            tmp.unit = [];
            tmp.threshold = [];
            save([spath fidsave '_' num2str(chunksize*(h-1)) 'to' num2str(ceil(max(time)/3600)) 'hr' '.mat'],'channel','time','threshold','waveform', 'unit');
            h = h+1;
        end
        
    end

end
keyboard        
end