% Jon Newman
% Georgia Tech
% 16 Feb 2010 - Basic data recall
% 17 Feb 2010 - Timing extraction for stimulation events
% 
% This script extracts data from TDT tanks that a filled during concurrent
% patch MEA experiments


%% Parameters needed to specify data to acess

tankID = 'C:\Documents and Settings\Patch Rig\Desktop\PROTOCOLS\20100208_concurrentPatchMEA\DataTanks\20100216'; % tank name
blockID = 'newman-1'; % block name

% Extracellular channel to look at
chan = 0; % channel to access, 0 = all channels

%% Open connection to server and extract information you want

%First instantiate a variable for the ActiveX wrapper interface
TT = actxcontrol('TTank.X')
% Then connect to a server.
TT.ConnectServer('Local', 'Me')
% Now open a tank for reading.
TT.OpenTank(tankID, 'R')
% Tell the server what channels you want to access
TT.SetGlobalV('Channel',chan);
% Select the block to access
TT.SelectBlock(blockID)

data = {};
% Tick data
N = TT.ReadEventsV(10000, 'StmV', chan, 0, 0.0, 0.0, 'ALL');
data.stimtime = TT.ParseEvV(0, N);

% Extract extracellular record
N = TT.ReadEventsV(10000, 'eNeu', chan, 0, 0.0, 0.0, 'ALL');
data.exrec.spiketime = TT.ParseEvInfoV(0, N, 6);
data.exrec.waveform = TT.ParseEvV(0, N);
data.exrec.channel = TT.ParseEvInfoV(0, N, 4);
data.exrec.unitID = TT.ParseEvInfoV(0, N, 5);

% Extract intracellular record
N = TT.ReadEventsV(10000, 'Heka', 1, 0, 0.0, 0.0, 'ALL');
data.inrec.vmon = TT.ParseEvV(0, N);
N = TT.ReadEventsV(10000, 'Heka', 2, 0, 0.0, 0.0, 'ALL');
data.inrec.imon = TT.ParseEvV(0, N);
N = TT.ReadEventsV(10000, 'Heka', 3, 0, 0.0, 0.0, 'ALL');
data.inrec.stim = TT.ParseEvV(0, N);

% Extract extracellular stimulus record
N = TT.ReadEventsV(10000, 'Stim', chan, 0, 0.0, 0.0, 'ALL');
data.stimulus = TT.ParseEvV(0, N);

% Close the tank and release client access to server
TT.CloseTank
TT.ReleaseServer


%% Examine Data

% plot(data.exrec.channel,data.exrec.spiketime,'.')
% plot(data.exrec.spiketime,data.exrec.channel'.')
% plot(data.exrec.spiketime,data.exrec.channel,'.')
% plot(data.exrec.waveform)
% plot(data.stimulus)
% plot(data.inrec.vmon)
% plot(data.inrec.imon)