function experiment(inData)

% loads the experiment figure and sets it up
% experiment
% experiment(startingDirectory)

% load the figure
	if ~isappdata(0, 'experiment')
        handles = guihandles(hgload('experiment.fig'));
        setappdata(0, 'experiment', handles.experiment);
		
		% set its location
		if ~ispref('locations', 'experiment')
			setpref('locations', 'experiment', [2960 743 236 417]);
		end	
		set(handles.experiment, 'position', getpref('locations', 'experiment'));
		
        if nargin == 1 && isnumeric(inData) && ismember(inData, [4 8 16])
            ttlSpecified = 1;
            numTTL = inData;
        else
            ttlSpecified = 0;
            numTTL = 4;
        end
		if nargin < 1 || isnumeric(inData)
			inData = pwd;
		end

		set(handles.mnuLoadProtocol, 'userData', inData);
		set(handles.mnuSaveProtocol, 'userData', inData);
		set(handles.mnuSetDataFolder, 'userData', inData);		
        set(handles.experiment, 'closeRequestFcn', @closeMe);
        
        % creat TTL check boxes
        switch numTTL
            case 4
                for i = 3:-1:0 % front panel
                    uicontrol('units', 'char', 'callback', 'saveExperiment;', 'style', 'check', 'parent', handles.pnlEnable, 'string', ['TTL ' num2str(i)], 'tag', 'ttlEnable', 'position', [1.6 + i * 10.6 2.05 9.4 1.1], 'toolTipString', ['TTL ' num2str(i)]);
                end
            case 8
                for i = 7:-1:0 % rear panel half
                    if (i + 1)/4 == round((i + 1)/4)
                        uicontrol('units', 'char', 'callback', 'saveExperiment;', 'style', 'check', 'parent', handles.pnlEnable, 'string', num2str(i), 'tag', 'ttlEnable', 'position', [1.6 + i * 5.3 2.05 5.5 1.1], 'toolTipString', ['TTL ' num2str(i)], 'backgroundColor', [0 0 1]);
                    else
                        uicontrol('units', 'char', 'callback', 'saveExperiment;', 'style', 'check', 'parent', handles.pnlEnable, 'string', num2str(i), 'tag', 'ttlEnable', 'position', [1.6 + i * 5.3 2.05 5.5 1.1], 'toolTipString', ['TTL ' num2str(i)]);
                    end
                end
            case 16
                for i = 15:-1:0 % rear panel full
                    if (i + 1)/4 == round((i + 1)/4)
                        uicontrol('units', 'char', 'callback', 'saveExperiment;', 'style', 'check', 'parent', handles.pnlEnable, 'string', '', 'tag', 'ttlEnable', 'position', [1.6 + i * 2.6 2.05 2.4 1.1], 'toolTipString', ['TTL ' num2str(i)], 'backgroundColor', [0 0 1]);
                    else                    
                        uicontrol('units', 'char', 'callback', 'saveExperiment;', 'style', 'check', 'parent', handles.pnlEnable, 'string', '', 'tag', 'ttlEnable', 'position', [1.6 + i * 2.6 2.05 2.4 1.1], 'toolTipString', ['TTL ' num2str(i)]);
                    end
                end
        end
        
        if ~ispref('experiment', 'internals')
            setpref('experiment', 'internals', {'-None-', 'Cs-methanesulfonate (19)', 'K-methanesulfonate (27)'});
            setpref('experiment', 'baths', {'-None-', '0 Mg', '3 mM K', '5 mM K'});
            setpref('experiment', 'drugs', {'-None-', 'APV (25 uM)', 'NBQX (5 uM)', 'TTX (1 uM)'});
			setpref('experiment', 'cellTypes', {'CA1', 'CA3', 'DGCL', 'Hilar', 'IML'});
			setpref('experiment', 'ttlTypes', {'SIU, tungsten, PP', 'Puff (5 mM Glu)'});
            currentAmps;
        end
        set(handles.internal, 'string', [getpref('experiment', 'internals') [char(173) 'Other' char(173)]]);
        set(handles.bath, 'string', [getpref('experiment', 'baths') [char(173) 'Other' char(173)]]);
        set(handles.drug, 'string', [getpref('experiment', 'drugs') [char(173) 'Other' char(173)]]);
        
        experimentCommands = loadMatlabText('experimentCommands.txt');
        set(handles.matlabCommand,'userData', {size(experimentCommands, 2) + 1, experimentCommands},'keyPressFcn', @commandKeyPress);
        
        saveExperiment;        
        
        % load the protocol gui
        loadProtocol(['defaultProtocol' num2str(numTTL) '.mat']);
        
        set(handles.cellTime, 'userData', clock);
        set(handles.episodeTime, 'userData', clock);
        set(handles.drugTime, 'userData', clock);
		clear experimentTimer;
        experimentTimer;
%         pause(.3); % to allow persistent variables and what not to be set
        start(timer('name', 'experimentClock', 'TimerFcn','experimentTimer', 'Period', 0.1, 'executionMode', 'fixedDelay', 'busyMode', 'queue'));
		
% 		if ispref('mitutoyo', 'xComm')
%             if ispref('ASI', 'commPort')
% 				switch questdlg('Both ASI and Mitutoyo indicators have been setup on this system.  Which would you like to use for location', '', 'ASI', 'Mitutoyo', 'ASI')
% 					case 'ASI'
% 						asiGui;
% 					case 'Mitutoyo'
% 						mitutoyoGui;
% 				end
% 			else
% 				mitutoyoGui;
%             end
%         else
%             if ispref('ASI', 'commPort')
%                 asiGui;
%             end
% 		end
else
    handles.experiment = figure(getappdata(0, 'runningProtocol'));
end    

onScreen(handles.experiment);    
if nargin < 1 || ttlSpecified
	setDataFolder;
else
	set(findobj('tag', 'mnuSetDataFolder'), 'userData', inData);
end

function commandText = loadMatlabText(fileName)
fid = fopen(fileName);
whichCommand = 1;
if fid > 0
    while 1
        tline = fgetl(fid);
        if ~ischar(tline)
            break
        end
        commandText{whichCommand} = tline;
        whichCommand = whichCommand + 1;
    end
    fclose(fid);
else
    commandText = '';
end

function addText(src, eventInfo)
userData = get(src, 'userData');
commandText = userData{2};
newCommand = cell2mat(get(src, 'string'));
if find(strcmp(commandText, newCommand))
    commandText(find(strcmp(commandText, newCommand)):length(commandText) - 1) = commandText(find(strcmp(commandText, newCommand)) + 1:length(commandText));
    commandText{length(commandText)} = newCommand;
    set(src, 'userData', {length(commandText), commandText});
else
    commandText{end + 1} = newCommand;
    set(src, 'userData', {length(commandText), commandText});		
end

function commandKeyPress(src, eventInfo)
userData = get(src, 'userData');
commandText = userData{2};
whichCommand = userData{1};

if whichCommand <= length(commandText) + 1 && whichCommand > -1
    if strcmp(eventInfo.Key, 'downarrow')  % down arrow
        if whichCommand < length(commandText)
            whichCommand = whichCommand + 1;
            set(src, 'string', commandText{whichCommand});
        elseif whichCommand == length(commandText)
            whichCommand = whichCommand + 1;
            set(src, 'string', '');
        end
    end

    if strcmp(eventInfo.Key, 'uparrow') && whichCommand > 1 % up arrow
        whichCommand = whichCommand - 1;
        set(src, 'string', commandText{whichCommand});
    end
end

set(src, 'userData', {whichCommand, commandText})

if strcmp(eventInfo.Key, 'return')
    handles = get(gcf, 'userdata');
    pause(.05);
    addText(src, eventInfo);
end    

function closeMe(varargin)
	stop(timerfind('name', 'experimentClock'));
    delete(timerfind('name', 'experimentClock'));
	if ~isempty(timerfind('name', 'repeatTimer'))
		stop(timerfind('name', 'repeatTimer'));
		delete(timerfind('name', 'repeatTimer'));
	end
	set(getappdata(0, 'runningProtocol'), 'units', 'pixel');
	setpref('locations', 'runningProtocol', get(getappdata(0, 'runningProtocol'), 'position'));		
	if isappdata(0, 'sealTest')
		close(getappdata(0, 'sealTest'))
	end
    
	if isappdata(0, 'bridgeBalance')
		close(getappdata(0, 'bridgeBalance'))
	end
	
    if isappdata(0, 'runningScope')
		close(getappdata(0, 'runningScope'))
    end
    delete(getappdata(0, 'runningProtocol'));
	
    rmappdata(0, 'experiment');
    rmappdata(0, 'currentExperiment');
    rmappdata(0, 'runningProtocol');
    rmappdata(0, 'currentProtocol');
    rmappdata(0, 'daScaleFactors');
    rmappdata(0, 'adScaleFactors');
	
	set(gcf, 'units', 'pixels');
	setpref('locations', 'experiment', get(gcf, 'position'));		
    delete(gcf);