function [mainWin, posStruct, shapeScreens, filledShapeScreens, shapeRects, textStruct, data_struct, totalTrial, blocksComplete, totalScore] = runShapeMTP(varargin)

expType = 2; %for use defining experiments when called with common function (i.e. in cases where you alternate blocks of trials)

white = [255 255 255];
black = [0 0 0];

if nargin == 1 %running this first or as stand-alone, so set up expt
    
    numBlocks = varargin{1};
    totalTrial = 1;
    blocksComplete = 0;
    totalScore = 0; %reset score on transition between tasks or not?
    
    %set up some inital parameters

    %SETUP OPTOTRAK
    %initialize optotrak
    optotrak_init

    %define coordinate frame for this experiment
    transform_info = optotrak_transform_realTime_dynamic;
    T_glob_loc = transform_info.T_glob_loc;

    %setup collection parameters for this experiment, which activates the markers
    setup_info = setup_optotrak

    %setup the participant, will put you in participant folder
    data_struct = setup_participant;

    %add the setup info and save
    data_struct.setup_info = setup_info;
    save(data_struct.participant_info.folderName, 'data_struct');

    %save the transform info now that there is a data_struct
    data_struct.transform_info = transform_info;
    save(data_struct.participant_info.folderName, 'data_struct');

    matData.participantFolder = cd;

    %create and cd to opto_raw folder
    mkdir('opto_raw');
    cd('opto_raw');

    matData.optoFolder = cd;

    %get info about the start position - participants will have to be within
    %5cm of this point to start the first trial
    reply='N';
    while ~strcmpi(reply,'Y')

        uiwait(msgbox('Press OK to collect initial start pos data','Collect?','modal'));

        %initialize and read one frame from all markers, but call marker 1 as start
        %pos info
        puFrameNumber  = 0;
        puElements=0;
        puFlags=0;
        pDataDest=single(zeros(1,setup_info.numMarkers*3)); %nMarkers x 3 (x,y,z for each)
        returnFlags=0;

        [returnFlags, puFrameNumber,puElements,puFlags,pDataDest] = DataGetNext3D(puFrameNumber,puElements,puFlags,pDataDest);

        [new_startPos]=transform4(T_glob_loc, pDataDest(1:3))

        Question = {'Start pos data:'...
            ''...
            'Marker 1: ' int2str(new_startPos)...
            ''...
            'Accept?'...
            ''};
        ButtonName = questdlg(Question, 'Start Pos Check', 'Yes', 'No', 'Quit', 'Yes');

        switch ButtonName
            case 'Yes'
                reply = 'Y';
                startPos = new_startPos;
            case 'No'
                reply = 'N';
            case 'Quit'
                %quit opto too?
                %need to pass quit flag so this actually works?
                clear all; close all; Screen('CloseAll'); 
                disp('*** Exiting Program ***');
                return
        end
    end
    matData.startPos{1} = startPos;

    %Make sure random seed is truly random
    rand('state',sum(100*clock));

    hidecursor;

    %open the screen
    [mainWin, mainRect] = screen('OpenWindow',2,white); %CHANGE HERE TO CHANGE SCREEN
    posStruct.centX = mainRect(3)/2;
    posStruct.centY = mainRect(4)/2;

    screenWidth = 930; %in mm, actual display width
    pixelWidth = mainRect(3);

    mmPerPixel = screenWidth/pixelWidth;

    mmTargetRadius = 10; %in mm
    pxlTargetRadius = mmTargetRadius / mmPerPixel;

    %define the position of the center of the hex on each side
    dist2HexCent = 90; %in mm
    pxl2HexCent = dist2HexCent / mmPerPixel;

    btwnTargDist = 30; %in mm
    pxlBtwnTarg = btwnTargDist / mmPerPixel;

    straightHexDist = cos(pi/6) * btwnTargDist;
    pxlStraightHex = straightHexDist / mmPerPixel;

    %center of right hex
    posStruct.RhexCentX = posStruct.centX+pxl2HexCent;
    posStruct.RhexCentY = posStruct.centY;

    %center of left hex
    posStruct.LhexCentX = posStruct.centX-pxl2HexCent;
    posStruct.LhexCentY = posStruct.centY;

    %note defining these here ASSUMES that monitor 2 is being used and that
    %monitor 1 has the same resolution as monitor 2
    posStruct.RCorrectArea = [mainRect(3) 0 mainRect(3) 0] + [posStruct.RhexCentX-3*pxlTargetRadius posStruct.RhexCentY-3*pxlTargetRadius posStruct.RhexCentX+3*pxlTargetRadius posStruct.RhexCentY+3*pxlTargetRadius];
    posStruct.LCorrectArea = [mainRect(3) 0 mainRect(3) 0] + [posStruct.LhexCentX-3*pxlTargetRadius posStruct.LhexCentY-3*pxlTargetRadius posStruct.LhexCentX+3*pxlTargetRadius posStruct.LhexCentY+3*pxlTargetRadius];
%     posStruct.RCorrectArea = [posStruct.RhexCentX-3*pxlTargetRadius posStruct.RhexCentY-3*pxlTargetRadius posStruct.RhexCentX+3*pxlTargetRadius posStruct.RhexCentY+3*pxlTargetRadius];
%     posStruct.LCorrectArea = [posStruct.LhexCentX-3*pxlTargetRadius posStruct.LhexCentY-3*pxlTargetRadius posStruct.LhexCentX+3*pxlTargetRadius posStruct.LhexCentY+3*pxlTargetRadius];


    numShapes = 6;
    %make shapes
    lineWidth = 4;
    boundBox = 100; %change if you need bigger shapes

    %1 = circle (all other shape areas will be based on the circle area)
    circDim = 2*pxlTargetRadius;
    circArea = pi*((circDim/2)^2);

    [shapeScreens(1),shapeRects{1}]=Screen('OpenOffscreenWindow',mainWin, white, [0 0 boundBox boundBox]);
    frameOvalOut(shapeScreens(1),[boundBox/2 boundBox/2],circDim,circDim,white,lineWidth,black);

    filledShapeScreens(1)=Screen('OpenOffscreenWindow',mainWin, white, [0 0 boundBox boundBox]);
    frameOvalOut(filledShapeScreens(1),[boundBox/2 boundBox/2],circDim,circDim,black,lineWidth,black);

    %2 = diamond
    dmndDim = sqrt(2*circArea);

    [shapeScreens(2),shapeRects{2}]=Screen('OpenOffscreenWindow',mainWin, white, [0 0 boundBox boundBox]);
    frameDiamondOut(shapeScreens(2),[boundBox/2 boundBox/2],dmndDim,dmndDim,white,lineWidth,black);

    [filledShapeScreens(2)]=Screen('OpenOffscreenWindow',mainWin, white, [0 0 boundBox boundBox]);
    frameDiamondOut(filledShapeScreens(2),[boundBox/2 boundBox/2],dmndDim,dmndDim,black,lineWidth,black);

    %3 = six pt star
    starDim = sqrt(2*circArea);

    [shapeScreens(3),shapeRects{3}]=Screen('OpenOffscreenWindow',mainWin, white, [0 0 boundBox boundBox]);
    frameSixPtStarOut(shapeScreens(3),[boundBox/2 boundBox/2],starDim,starDim,white,lineWidth,black);

    [filledShapeScreens(3)]=Screen('OpenOffscreenWindow',mainWin, white, [0 0 boundBox boundBox]);
    frameSixPtStarOut(filledShapeScreens(3),[boundBox/2 boundBox/2],starDim,starDim,black,lineWidth,black);

    %4 = X
    exDim = sqrt(8/7*circArea);

    [shapeScreens(4),shapeRects{4}]=Screen('OpenOffscreenWindow',mainWin, white, [0 0 boundBox boundBox]);
    frameExOut(shapeScreens(4),[boundBox/2 boundBox/2],exDim,exDim,sqrt(2*exDim^2)/4,white,lineWidth,black);

    [filledShapeScreens(4)]=Screen('OpenOffscreenWindow',mainWin, white, [0 0 boundBox boundBox]);
    frameExOut(filledShapeScreens(4),[boundBox/2 boundBox/2],exDim,exDim,sqrt(2*exDim^2)/4,black,lineWidth,black);

    %5 = +
    crossDim = sqrt(16/7*circArea);

    [shapeScreens(5),shapeRects{5}]=Screen('OpenOffscreenWindow',mainWin, white, [0 0 boundBox boundBox]);
    frameCrossOut(shapeScreens(5),[boundBox/2 boundBox/2],crossDim,crossDim,crossDim/4,white,lineWidth,black);

    [filledShapeScreens(5)]=Screen('OpenOffscreenWindow',mainWin, white, [0 0 boundBox boundBox]);
    frameCrossOut(filledShapeScreens(5),[boundBox/2 boundBox/2],crossDim,crossDim,crossDim/4,black,lineWidth,black);

    %6 = square
    squareDim = sqrt(circArea);

    [shapeScreens(6),shapeRects{6}]=Screen('OpenOffscreenWindow',mainWin, white, [0 0 boundBox boundBox]);
    frameRectOut(shapeScreens(6),[boundBox/2 boundBox/2],squareDim,squareDim,white,lineWidth,black);

    [filledShapeScreens(6)]=Screen('OpenOffscreenWindow',mainWin, white, [0 0 boundBox boundBox]);
    frameRectOut(filledShapeScreens(6),[boundBox/2 boundBox/2],squareDim,squareDim,black,lineWidth,black);

    %assign shapes to value (win/loss) / predictive conditions
    %valueAssign(1) = highProbWin
    %valueAssign(2) = lowProbWin
    %valueAssign(3) = highProbLoss
    %valueAssign(4) = lowProbLoss
    %valueAssign(5) = neutral1
    %valueAssign(6) = neutral2
    valueAssign = randPerm(numShapes);
    matData.valueAssign = valueAssign;

    %set up the text parameters and text
    SCREEN('TextFont', mainWin, 'Arial');
    SCREEN('TextSize', mainWin, 100);

    textStruct.blockText = 'Space to start block X';
    [textStruct.blockX,textStruct.blockY] = textCtr(mainWin,textStruct.blockText);
    textStruct.blockText = 'Space to start block ';

    textStruct.correct = 'XX / XX';
    [textStruct.corrX,textStruct.corrY] = textCtr(mainWin,textStruct.correct);

    textStruct.scoreText = '3333';
    [textStruct.scoreX,textStruct.scoreY] = textCtr(mainWin,textStruct.scoreText);

    textStruct.blockScoreText = 'Block Score = 3333';
    [textStruct.BscoreX,textStruct.BscoreY] = textCtr(mainWin,textStruct.blockScoreText);

    textStruct.totalScoreText = 'Total Score = 3333';
    [textStruct.TscoreX,textStruct.TscoreY] = textCtr(mainWin,textStruct.totalScoreText);

    textStruct.tooEarly = 'Too Early!';
    [textStruct.earlyX,textStruct.earlyY] = textCtr(mainWin,textStruct.tooEarly);

    textStruct.timeOut = 'Time Out';
    [textStruct.toX,textStruct.toY] = textCtr(mainWin,textStruct.timeOut);

    textStruct.tooSlow = 'Too Slow';
    [textStruct.slowX,textStruct.slowY] = textCtr(mainWin,textStruct.tooSlow);

    textStruct.miss = 'Miss';
    [textStruct.missX,textStruct.missY] = textCtr(mainWin,textStruct.miss);

    textStruct.good = 'Good';
    [textStruct.goodX,textStruct.goodY] = textCtr(mainWin,textStruct.good);

else %you are NOT running this for the first time
    
    numBlocks = varargin{1};
    mainWin = varargin{2};
    posStruct = varargin{3};
    shapeScreens = varargin{4};
    filledShapeScreens = varargin{5};
    shapeRects = varargin{6};
    textStruct = varargin{7};
    data_struct = varargin{8};
    totalTrial = varargin{9};
    blocksComplete = varargin{10};
    totalScore = varargin{11};
    
    matData = data_struct.matData;
    fdaMat = data_struct.fdaMat;
    
end

beep = makebeep(1000,0.15);

%set up the number of trial types for each condition PER BLOCK
%valueTargL valueTargR endSide numReps
configAndProb = [
    1 2 0 2 %e.g. highProbWinL lowProbWinR endL 10 times
    2 1 0 2
    3 4 0 2
    4 3 0 2
    5 6 0 2
    6 5 0 2
    1 3 0 2
    3 1 0 2
    2 4 0 2
    4 2 0 2
    1 2 1 2 
    2 1 1 2
    3 4 1 2
    4 3 1 2
    5 6 1 2
    6 5 1 2
    1 3 1 2
    3 1 1 2
    2 4 1 2
    4 2 1 2
];

%points for different trial types
goodPoints = 2;
tooSlowPoints = 0;
missPoints = -2;
timeOutPoints = -2;
tooEarlyPoints = -2;

tooEarlyLimit = 0.1;
rxnTimeLimit = 0.325;
mvmtTimeLimit = 0.425;

block = 1; %for keeping track of this sequence of blocks

while block <= numBlocks
    
    %make a trial array of all of the trials defined in configAndProb, then randomize
    preRandomTrialArray = [];
    trialArray = [];
    for i = 1:size(configAndProb,1)
        curConfigAndProb = configAndProb(i,:);
        curAdding = curConfigAndProb(4); %number of trials of this type, total
        preRandomTrialArray = [preRandomTrialArray ones(1,curAdding)*i];
    end

    %randomize
    randOrder = randPerm(length(preRandomTrialArray));
    trialArray(randOrder) = preRandomTrialArray;
    
    curBlock = block + blocksComplete;
    screen('DrawText',mainWin,[textStruct.blockText int2str(curBlock)],textStruct.blockX,100,[0 0 0]);
    %add in instructions specific to this experiment
    SCREEN('TextSize', mainWin, 50);
    curText = 'In this block, you need to move as quickly as possible';
    [curTx,curTy] = textCtr(mainWin,curText);
    screen('DrawText',mainWin,curText,curTx,430,[0 0 0]);
    curText = 'One target will fill in once you start moving';
    [curTx,curTy] = textCtr(mainWin,curText);
    screen('DrawText',mainWin,curText,curTx,530,[0 0 0]);
    curText = 'Touch the filled target quickly to receive points';
    [curTx,curTy] = textCtr(mainWin,curText);
    screen('DrawText',mainWin,curText,curTx,630,[0 0 0]);
    SCREEN('TextSize', mainWin, 100);
    screen('Flip',mainWin);
    waitsecs(1);
    
    touch = 0;
    touchFlag = 0;
    %Wait for spacebar press or touchscreen to advance, esc to quit
    while ~touchFlag
        [touchFlag,x,y,buttons] = detectTouch(mainWin);
        touchFlag;
        [touch,secs,keyCode] = KbCheck;
        
        if find(keyCode) == 32
            break;
        end

        if find(keyCode) == 27 %ESCAPE
            %screen('closeAll');
            return;
        end
        
    end
    
    numTrialsPerBlock = length(trialArray);
    %numTrialsPerBlock = 2;
    numCorrect = 0;
	trial = 1; %for keeping track of this segment of trials
    
	while trial <= numTrialsPerBlock
        
        %initialize the error array.
        %error(1) = tooEarly;
        %error(2) = timeOut;
        %error(3) = tooSlow;
        %error(4) = miss;
        %error(5) = analyzeReach error;
        error = [0 0 0 0 0];
        curTrialString = '    ';
        
        %update trial counters
        trialCtr = totalTrial
        totalTrial = totalTrial+1;
        
        %clear the screen
        screen('fillRect',mainWin,white);
        screen('Flip',mainWin);
        
        %make sure hand is close to start position
        cd(matData.optoFolder);
        trial_fileName = [data_struct.participant_info.folderName '_' int2str(trialCtr) '.dat'];
    
        OptotrakActivateMarkers();

        %wait for IR1 to be within startPos range
        inRange = 20; %say 20mm in 3D distance is within range
        inRangeFlag = 0;
        while ~inRangeFlag

            %initialize and read one frame from all markers, but call marker 1 as start
            %pos info
            puFrameNumber  = 0;
            puElements=0;
            puFlags=0;
            pDataDest=single(zeros(1,data_struct.setup_info.numMarkers*3)); %nMarkers x 3 (x,y,z for each)
            returnFlags=0;

            [returnFlags, puFrameNumber,puElements,puFlags,pDataDest] = DataGetNext3D(puFrameNumber,puElements,puFlags,pDataDest);
            curLocation = pDataDest(1:3);
            curLocation=transform4(data_struct.transform_info.T_glob_loc, curLocation);
            curRange = sqrt(sum((curLocation-matData.startPos{1}).^2));

            if curRange < inRange
                inRangeFlag = 1;
            end
        end
        
        %Put up a fixation (use small dot)
        screen('fillOval',mainWin,black,[posStruct.centX-5, posStruct.centY-5, posStruct.centX+5, posStruct.centY+5]);
        screen('Flip',mainWin);
        
        %Wait some random amount of time (btwn 1-2s),then remove fixation
        curWait = 1+rand;
        waitsecs(curWait);

        %Remove fixation, draw the targets for this trial
        screen('fillOval',mainWin,white,[posStruct.centX-5, posStruct.centY-5, posStruct.centX+5, posStruct.centY+5]);

        curTrialType = trialArray(trial);

        curConfigAndProb = configAndProb(curTrialType,:);
        curTargLeft = curConfigAndProb(1);
        curTargRight = curConfigAndProb(2);
        curEndSide = curConfigAndProb(3);

        curShapeScreenL = shapeScreens(matData.valueAssign(curTargLeft));
        curShapeRectL = shapeRects{matData.valueAssign(curTargLeft)};
        screen('CopyWindow',curShapeScreenL,mainWin,curShapeRectL,[posStruct.LhexCentX-curShapeRectL(3)/2 posStruct.LhexCentY-curShapeRectL(4)/2 posStruct.LhexCentX+curShapeRectL(3)/2 posStruct.LhexCentY+curShapeRectL(4)/2]);

        curShapeScreenR = shapeScreens(matData.valueAssign(curTargRight));
        curShapeRectR = shapeRects{matData.valueAssign(curTargRight)};
        screen('CopyWindow',curShapeScreenR,mainWin,curShapeRectR,[posStruct.RhexCentX-curShapeRectR(3)/2 posStruct.RhexCentY-curShapeRectR(4)/2 posStruct.RhexCentX+curShapeRectR(3)/2 posStruct.RhexCentY+curShapeRectR(4)/2]);

        %start recording optotrak
        %initialize the file for spooling
        DataBufferInitializeFile(0,trial_fileName);

        %start collecting data
        DataBufferStart();

        screen('Flip',mainWin,0,1);
        %play a beep
        snd('Play',beep);

        targetOnTime = getsecs;
        
        %wait for movement
        [tooEarlyFlag reachStartedFlag reachOnsetPos] = detectVelOnset_opto_realTime(data_struct.setup_info,data_struct.transform_info.T_glob_loc,rxnTimeLimit,matData.startPos{1});
        reachOnset = getsecs;
        
        %determine if they left too early
        if reachOnset-targetOnTime < tooEarlyLimit | tooEarlyFlag
            tooEarlyFlag = 1;
            curTrialScore = tooEarlyPoints;
            error(1) = 1;
            text = textStruct.tooEarly;
            tx = textStruct.earlyX;
            ty = textStruct.earlyY;
            tColor = [0 255 0];
        end
        
        %or if they didn't leave in time
        if ~reachStartedFlag
            curTrialScore = timeOutPoints;
            error(2) = 1;
            text = textStruct.timeOut;
            tx = textStruct.toX;
            ty = textStruct.toY;
            tColor = [255 0 255];
        end
        
        %if they didn't leave too early and they left before the rxnTime
        %limit, fill in the correct target for this trial type
        correctArea = [0 0 0 0];
        RHit = 0;
        LHit = 0;
        if ~tooEarlyFlag & reachStartedFlag
            if curEndSide %==1 therefore right
                correctArea = posStruct.RCorrectArea;
                curShapeScreenR = filledShapeScreens(matData.valueAssign(curTargRight));
                curShapeRectR = shapeRects{matData.valueAssign(curTargRight)};
                screen('CopyWindow',curShapeScreenR,mainWin,curShapeRectR,[posStruct.RhexCentX-curShapeRectR(3)/2 posStruct.RhexCentY-curShapeRectR(4)/2 posStruct.RhexCentX+curShapeRectR(3)/2 posStruct.RhexCentY+curShapeRectR(4)/2]);
            else %left select        
                correctArea = posStruct.LCorrectArea;
                curShapeScreenL = filledShapeScreens(matData.valueAssign(curTargLeft));
                curShapeRectL = shapeRects{matData.valueAssign(curTargLeft)};
                screen('CopyWindow',curShapeScreenL,mainWin,curShapeRectL,[posStruct.LhexCentX-curShapeRectL(3)/2 posStruct.LhexCentY-curShapeRectL(4)/2 posStruct.LhexCentX+curShapeRectL(3)/2 posStruct.LhexCentY+curShapeRectL(4)/2]);
            end
            screen('Flip',mainWin,0,1);
        end
           
        %Wait for them to hit the screen
        touchFlag = 0;
        x=0;
        y=0;
        while ~touchFlag & ~tooEarlyFlag & reachStartedFlag

            [touchFlag,x,y,buttons] = detectTouch(mainWin);

        end
        endPos = [x y];
        touchedScreenTime = getsecs;
        
        %this is here so outputs match across tasks
        LHit = 0;
        RHit = 0;      
        if ~checkMiss(x,y,posStruct.LCorrectArea)
            LHit = 1;
            RHit = 0;
        else
            LHit = 0;
            RHit = 1;
        end
        
                
        tooSlowFlag = 0;
        if touchedScreenTime - reachOnset > mvmtTimeLimit
            error(3) = 1;
            tooSlowFlag = 1;
            curTrialScore = tooSlowPoints;
            text = textStruct.tooSlow;
            tx = textStruct.slowX;
            ty = textStruct.slowY;
            tColor = [255 0 0];
        end
               
        missFlag = 0;
        if (checkMiss(x,y,correctArea)) & ~tooEarlyFlag & reachStartedFlag %note this means that miss preempts tooSlow as an error - this needs to happen otherwise you can end up analyzing miss trials when including tooSlow trials
            error(4) = 1;
            missFlag = 1;
            curTrialScore = missPoints;
            text = textStruct.miss;
            tx = textStruct.missX;
            ty = textStruct.missY;
            tColor = black;
        end
        
        %if they made it through with no errors, its a good trial
        if ~any(error)
            curTrialScore = goodPoints;
            text = textStruct.good;
            tx = textStruct.goodX;
            ty = textStruct.goodY;
            tColor = [0 0 255];
        end
        
        %clear the screen
        screen('fillRect',mainWin,white);
        screen('DrawText',mainWin,text,tx,ty,tColor);
        screen('Flip',mainWin,0,1);
        if tooEarlyFlag | ~reachStartedFlag
            waitsecs(1);
        else
            waitsecs(0.5);
        end

        oldBlockScore = totalScore;
        totalScore = totalScore + curTrialScore;

        oldBlockString = '    ';
        oldBlockString(end-length(int2str(oldBlockScore))+1:end) = int2str(oldBlockScore);
        curBlockString = '    ';
        curBlockString(end-length(int2str(totalScore))+1:end) = int2str(totalScore);

        curTrialString(end-length(int2str(curTrialScore))+1:end) = int2str(curTrialScore);
        
        if curTrialScore < 0
            color = [255 0 0];
        elseif curTrialScore == 0
            color = black;
        else
            color = [0 0 255];
        end
        
        for i = 1:30
            if i > 1
                screen('fillRect',mainWin,white,[textStruct.scoreX textStruct.scoreY+150+(i-1)*2 textStruct.scoreX+300 textStruct.scoreY+150+(i-1)*2+150]);
            end
            screen('DrawText',mainWin,oldBlockString,textStruct.scoreX,textStruct.scoreY+300,black);
            screen('DrawText',mainWin,curTrialString,textStruct.scoreX,textStruct.scoreY+150+i*2,color);
            screen('Flip',mainWin,0,1);
        end

        screen('fillRect',mainWin,white,[textStruct.scoreX textStruct.scoreY+150 textStruct.scoreX+300 textStruct.scoreY+450]);
        screen('DrawText',mainWin,curBlockString,textStruct.scoreX,textStruct.scoreY+300,color);
        screen('Flip',mainWin);
        
        %spool data from buffer to file
        puRealtimeData=0;
        puSpoolComplete=0;
        puSpoolStatus=0;
        pulFramesBuffered=0;
        while (puSpoolComplete == 0)
            [puRealtimeData,puSpoolComplete,puSpoolStatus,pulFramesBuffered]=DataBufferWriteData(puRealtimeData,puSpoolComplete,puSpoolStatus,pulFramesBuffered);
            WaitSecs(.1);
            if puSpoolStatus ~= 0
                disp('Spooling Error');
                break;
            end % if
        end

        OptotrakDeActivateMarkers();

        close all;
        %can maybe get error here
        [reach_info normalizedReach] = analyzeReach(trial_fileName, data_struct.transform_info.T_glob_loc, matData.startPos, data_struct.setup_info.frameRate);
        
        %repack reach_info        
       
        %for stuff that only gets set once
        data_struct.reach_info.posLowpassCutoff = reach_info.posLowpassCutoff;
        data_struct.reach_info.velLowpassCutoff = reach_info.velLowpassCutoff;
        data_struct.reach_info.normalizeFrames = reach_info.normalizeFrames;
        data_struct.reach_info.normalizeType = reach_info.normalizeType;
        
        %for stuff taht updates every trial
        data_struct.reach_info.screenPos{trialCtr} = reach_info.screenPos{1};
        data_struct.reach_info.fill_info.startPos{trialCtr} = reach_info.fill_info.startPos{1};
        data_struct.reach_info.fill_info.altFill{trialCtr} = reach_info.fill_info.altFill{1};
        data_struct.reach_info.fill_info.linFill{trialCtr} = reach_info.fill_info.linFill{1};
        data_struct.reach_info.startFrame(trialCtr) = reach_info.defined_reach_info.startFrame;
        data_struct.reach_info.endFrame(trialCtr) = reach_info.defined_reach_info.endFrame;
        
        %clear the screen
        screen('fillRect',mainWin,white);
        screen('Flip',mainWin);       
        
        %package into an appropriate fda matrix
        %NOTE do not construct grouping variable here since number of
        %subjects is unknown.  ALSO NOTE fdaMat will have error trials
        %included at this stage.  This means after collection of all data
        %from all subs, another script will have to run that creates a
        %final fdaMat and group for the entire experiment.
        fdaMat.x(trialCtr,:) = normalizedReach{1}(:,1);
        fdaMat.y(trialCtr,:) = normalizedReach{1}(:,2);
        fdaMat.z(trialCtr,:) = normalizedReach{1}(:,3);
        fdaMat.velX(trialCtr,:) = normalizedReach{1}(:,4);
        fdaMat.velY(trialCtr,:) = normalizedReach{1}(:,5);
        fdaMat.velZ(trialCtr,:) = normalizedReach{1}(:,6);
        fdaMat.tangVel(trialCtr,:) = sqrt(normalizedReach{1}(:,4).^2+normalizedReach{1}(:,5).^2+normalizedReach{1}(:,6).^2)';
        
        %get the velocity measures
        [curPeakV,curTtpv] = max(fdaMat.tangVel);
        curPeakV = curPeakV(1) * data_struct.setup_info.frameRate; %convert from mm/frame to mm/s
        curTtpv = curTtpv(1)/data_struct.setup_info.frameRate; %convert from frames to s
        
        matData.block(trialCtr) = curBlock;
        matData.expType(trialCtr) = expType;
        matData.waitTime(trialCtr) = curWait;
        matData.rxnTime(trialCtr) = reachOnset - targetOnTime;
        matData.reachOnsetPos(trialCtr,:) = reachOnsetPos;
        matData.mvmtTime(trialCtr) = touchedScreenTime - reachOnset;
        matData.peakV(trialCtr) = curPeakV;
        matData.ttpv(trialCtr) = curTtpv;
        matData.trialType(trialCtr) = curTrialType;
        matData.curTargL(trialCtr) = curTargLeft;
        matData.curTargR(trialCtr) = curTargRight;
        matData.curShapeL(trialCtr) = matData.valueAssign(curTargLeft);
        matData.curShapeR(trialCtr) = matData.valueAssign(curTargRight);
        matData.endPos(trialCtr,:) = endPos;
        matData.hitVector(trialCtr,:) = [LHit RHit];
        matData.winVector(trialCtr,:) = [0 0]; %winning and losing on a side of space is not meaningful here, but do so outputs are consistent
        matData.trialScore(trialCtr) = curTrialScore;
        matData.error(trialCtr,:) = error;
        
        data_struct.matData = matData;
        data_struct.fdaMat = fdaMat;
        
        %save data with the new trial data
        cd(matData.participantFolder);
        save(data_struct.participant_info.folderName, 'data_struct');
        
        trial = trial + 1;
    end
    block = block + 1;
end
blocksComplete = blocksComplete + numBlocks;


        

    
    


    

