clear all;
close all;

dirID = 'C:\Documents and Settings\mingfaifong\My Documents\worm\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 = {[5,6],[11,12],[14,15],[17,18],[23,24]};
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 everything
save worm_alldata dat fidlist