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

cultureID = 'juju1';
dirID = 'C:\Users\mingfaifong\Documents\2012-10-30_juju1_13DIV_Patch_post-vehicle\ascii\';
y = dir(dirID); z = [y.isdir];
fidlist = {y(~z).name}';

load(['juju1_nameswap.mat']);

%% replacing all commas with nothing
for i = 1:length(fidlist)
    fid = fidlist{i};
    new_name = name_id{find(num_id==str2num(fidlist{i}(1:end-4)))}(1:end-5);
    replaceinfile(',','',[dirID fid],[dirID new_name]);
end

%% importing data
clear all;
close all;
cultureID = 'juju1';
dirID = 'C:\Users\mingfaifong\Documents\2012-10-30_juju1_13DIV_Patch_post-vehicle\ascii\';
y = dir(dirID); z = [y.isdir];
fidlist = {y(~z).name}';

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 = {[1,2],[3,4],[8,9],[10,11],[14,15],[18,19,20],[21,22,23],[24,25],[26,27],[28,29],[30,31,32]};
i_remove = [5,6,7,12,16,17];
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');