%% extracting spike data and saving by well
clear all;
fpath = 'C:\Users\USER\Documents\mingfaifong\2012-09-04_fatty21368_MEA_6well\';
%cd('C:\Users\USER\Documents\mingfaifong\2012-09-04_fatty21368_MEA_6well');
fid = 'fatty21368_19DIV_nodrug_01';
x = loadspk([fpath,fid,'.spk'])

w(1,:) = [3 4 5 12 13 20 21 28 29];
w(2,:) = [7 14 15 16 22 23 24 30 31];
w(3,:) = [38 39 40 46 47 48 55 56 63];
w(4,:) = [36 37 44 45 52 53 60 61 62];
w(5,:) = [34 35 41 42 43 49 50 51 58];
w(6,:) = [2 9 10 17 18 19 25 26 27];

for i = 1:6
    excludechannels = [1 6 8 11 32 33 54 57 59 64]; %corners and grounds
    switch i
        case 1
            excludechannels = horzcat(excludechannels,w(2,:),w(3,:),w(4,:),w(5,:),w(6,:));
            well = 'A';
        case 2
            excludechannels = horzcat(excludechannels,w(1,:),w(3,:),w(4,:),w(5,:),w(6,:));
            well = 'B';
        case 3
            excludechannels = horzcat(excludechannels,w(1,:),w(2,:),w(4,:),w(5,:),w(6,:));
            well = 'C';
        case 4
            excludechannels = horzcat(excludechannels,w(1,:),w(2,:),w(3,:),w(5,:),w(6,:));
            well = 'D';
        case 5
            excludechannels = horzcat(excludechannels,w(1,:),w(2,:),w(3,:),w(4,:),w(6,:));
            well = 'E';
        case 6
            excludechannels = horzcat(excludechannels,w(1,:),w(2,:),w(3,:),w(4,:),w(5,:));
            well = 'F';
    end

    ind = find(ismember(x.channel,excludechannels)==0);
    time = x.time(ind);
    channel = x.channel(ind);
    unit = x.unit(ind);
    waveform = x.waveform(:,ind);
    threshold = x.threshold(ind);

    save([fpath,fid,well,'.mat'],'time','channel','unit','waveform','threshold');

end

% cd C:\Users\USER\Documents\mingfaifong\2012-09-04_fatty21368_MEA_6well
% spkdat = loadspk('fatty21368_19DIV_nodrug_01.spk');
% edit loadspk
% edit loadspike
% spkdat = loadspike('fatty21368_19DIV_nodrug_01.spk');
% spkdat = loadspk('fatty21368_19DIV_nodrug_01.spk');
% SS = SqueakySpk('fatty21368_19DIV_nodrug_01.spk',2500,1/1200,spkdat);

%% trying waveclus on single well
spkdat = load('C:\Users\USER\Documents\mingfaifong\2012-09-04_fatty21368_MEA_6well\fatty21368_19DIV_nodrug_01C.mat');
SS = SqueakySpk('fatty21368_19DIV_nodrug_01C',2500,1/1200,spkdat);