clear all;
close all;

cultureID = {'xeko2','xeko3','xeko4'};
dirID = 'C:\Documents and Settings\mingfaifong\My Documents\xeko234\ascii\';
y = dir(dirID); z = [y.isdir];
fidlist = {y(~z).name}';

%% replacing all commas with nothing
for i = 1:length(fidlist)
    fid = fidlist{i};
    replaceinfile(',','',[dirID fid],'-nobak');
end

%% importing data
for i = 1:length(fidlist)
    fid = fidlist{i};
    dat{i,1} = importdata([dirID fid]);
    dat{i,1} = sortrows(dat{i},2);
end

%% combine files from same recording
i_combine = {[3,4],[13,14],[17,18],[19,20],[22,23],[25,26],[27,28]};
i_remove = [];
for i = 1:length(i_combine)
    for j = 1:length(i_combine{i})-1
        dat{i_combine{i}(1)} = [dat{i_combine{i}(1)}; dat{i_combine{i}(j+1)}];
        i_remove(end+1) = i_combine{i}(j+1)
        fidlist{i_combine{i}(1)} = [fidlist{i_combine{i}(1)},'; ',fidlist{i_combine{i}(j+1)}]
    end
end
dat(i_remove) = [];
fidlist(i_remove) = [];

%% add frequencies in Hz
for i = 1:length(dat)
    dat{i,1}(:,19) = [1000./diff(dat{i,1}(:,2)); NaN]; %frequency in Hz
end

%% save data by cultureID
dat_tmp = dat;
for i = 1:length(cultureID)
    i_match = strncmpi( repmat(cultureID{i},size(fidlist,1),size(fidlist,2)),fidlist,length(cultureID{i}));
    dat = dat_tmp(i_match,:);
    fid = fidlist(i_match,:);
    save(['C:\Documents and Settings\mingfaifong\My Documents\xeko234\' cultureID{i} '_alldata'],'dat','fidlist');
end