function [] = replayPerformance;

for studyNum = 1 : 2
    
    % load data
    load(sprintf('dataStudy%d_buttons_replay',studyNum),'buttonStates','buttonTimes');
    load(sprintf('dataStudy%d_stimPhases_replay',studyNum),'stimPhaseStart','stimPhaseEnd','stimPhaseType');
    
    hitRate = nan(1,size(buttonStates,1));
    
    for subjectCount = 1 : size(buttonStates,1)
        
        
        % collect the times
        allCorrect   = []; % correct exclusive button
        allIncorrect = []; % incorrect exclusive button
        allOther     = []; % other button
        
        for blockCount = 1 : size(buttonStates,2)
            
            % for comparision to stim Type, buttons need to be recoded from 1,2,4,8 to 1,2,3,4, everything
            % else (non-exclusive states are set to 0)
            currentStates = buttonStates{subjectCount,blockCount};
            currentTimes  = buttonTimes{subjectCount,blockCount};
            
            buttonStatesRecoded = nan(1,numel(currentStates));
            idxNone = find((currentStates~=1)&(currentStates~=2)&(currentStates~=4)&(currentStates~=8));
            buttonStatesRecoded(idxNone)=0;
            buttonStatesRecoded(currentStates==1)=1;
            buttonStatesRecoded(currentStates==2)=2;
            buttonStatesRecoded(currentStates==4)=3;
            buttonStatesRecoded(currentStates==8)=4;
            clear currentStates;
            
            % short hand notation
            currentStart = stimPhaseStart{subjectCount,blockCount};
            currentEnd   = stimPhaseEnd{subjectCount,blockCount};
            currentType  = stimPhaseType{subjectCount,blockCount};
            
            % loop over phases of the same stimuliation
            for k = 1 : length(currentType)
                
                % last index of buttons before phase stats
                lastIdxBeforeStart = max(find(currentTimes<=currentStart(k)));
                
                % buttons pressed during phases
                idxDuring = find(...
                    (currentTimes>currentStart(k))&...
                    (currentTimes<=currentEnd(k)));
                
                statesInPhase = ...
                    [...
                    buttonStatesRecoded(lastIdxBeforeStart),...
                    buttonStatesRecoded(idxDuring)];
                
                timesInPhase = ...
                    [currentStart(k),currentTimes(idxDuring),currentEnd(k)];
                
                durationsInPhase = ...
                    diff(timesInPhase); % this has same length as idx
                
                
                % determine correct button, incorrect button and undefined states
                correctIdx   = find(statesInPhase==currentType(k));
                incorrectIdx = ...
                    find((statesInPhase~=currentType(k))&(statesInPhase>0));
                otherIdx = find(statesInPhase==0);
                
                
                % durations
                correctDur   = durationsInPhase(correctIdx);
                incorrectDur = durationsInPhase(incorrectIdx);
                otherDur     = durationsInPhase(otherIdx);
                
                % collect over all phases
                allCorrect   = [allCorrect, sum(correctDur)];
                allIncorrect = [allIncorrect, sum(incorrectDur)];
                allOther     = [allOther, sum(otherDur)];
                
            end
            
        end
        
        hitrate(subjectCount) = 100*(sum(allCorrect)/(sum(allCorrect)+sum(allIncorrect)));
        
    end
    
    fprintf('Hit rate Study %d: %4.1f%% (SD: %4.1f%%)\n',studyNum,mean(hitrate),std(hitrate));
    
    
end