clear;

fpath = ['C:\Users\USER\Documents\mingfaifong\gatsby363\matfiles\originals\aux_gnd_removed\suspicious\'];
spath = ['C:\Users\USER\Documents\mingfaifong\gatsby363\matfiles\'];
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

%badchan = [1 8 33 57 64];
badchan = [10 11 25 31 34 35 47 53 58];

for i = 1:length(fidlist)
    fid = fidlist{i};
    x = load([fpath fid],'-mat');
    ind_goodchan = find(x.channel~=badchan(1) & x.channel~=badchan(2) & x.channel~=badchan(3) & x.channel~=badchan(4) & x.channel~=badchan(5) & x.channel~=badchan(6) & x.channel~=badchan(7) & x.channel~=badchan(8) & x.channel~=badchan(9));
    %ind_goodchan = find(x.channel~=1 & x.channel~=8 & x.channel~=33 & x.channel~=57 & x.channel~=64);
    time = x.time(ind_goodchan);
    channel = x.channel(ind_goodchan);
    waveform = x.waveform(:,ind_goodchan);
    threshold = x.threshold(ind_goodchan);
    %keyboard;
    save([spath fid(1:length(fid)-4) '.mat'],'time','channel','waveform','threshold');
    clearvars -except i fidlist fpath spath badchan
end