clear all;
close all;
%addpath('C:\Documents and Settings\mingfaifong\My Documents\MiniAnalysis Data\shared_functions');

cultureID = 'wingnut1';
dirID = 'C:\Users\mingfaifong\Documents\2013-02-05_wingnut1_12DIV_Patch_post-vehicle\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 = {[6,7],[11,12]};
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;
save([dirID(1:length(dirID)-6) cultureID '_alldata'],'dat','fidlist');