function [] = figure3;

%
% function [] = figure3;
%
% plots raw version of figure 3 of Einhäuser, W., Methfessel, P., & Bendixen, A. (2017). Newly acquired audio-visual associations bias perception in binocular rivalry. Vision Research, 133, 121-129. doi: 10.1016/j.visres.2017.02.001
%
% 

% load data file
load eyeTraces oknGain 

% load files containing the conditions per observer and per block and trial
load toneByBlockAndTrail toneByBlockAndTrial
load conditionsByObserver ...
	lowPitchColorByObserver ...
	highPitchColorByObserver ...
	lowPitchDirectionByObserver ...
	highPitchDirectionByObserver

% compute the bias for each observer and tone
gainLowPitchTone  = nan(1,16);
gainHighPitchTone = nan(1,16);
gainNoTone        = nan(1,16);

for obsNum = 1 : 16

  % to aggregate the data over all blocks and trials (which can
  % contain slightly varying amounts of valid data) additional
  % variables are needed to sum the data first and later compute the
  % mean over all trials and blocks

  % for the trials, in which the low-pitch tone is presented
  sumLowPitchTone = 0;       % sum of gain over all valid sample points
  validDataLowPitchTone = 0; % total amput of valid sample points 

  % for the trials, in which the high-pitch tone is presented
  sumHighPitchTone = 0;       % sum of gain over all valid sample points
  validDataHighPitchTone = 0; % total amput of valid sample points 
  
  % for the trials, in which no tone is presented
  sumNoTone = 0;          % sum of gain over all valid sample points
  validDataNoTone    = 0;  % total amput of valid sample points 

  for block = 1 : 6
	
	for trial = 1 : 3
	 
	  % add data to the correct tone condition
	  if (toneByBlockAndTrial(obsNum,block,trial)==1)
		
		% low-pitch tone was played in trial
		sumLowPitchTone = sumLowPitchTone + ...
			nansum(oknGain{obsNum,block,trial});
		
		validDataLowPitchTone = validDataLowPitchTone  + ...
			sum(isfinite(oknGain{obsNum,block,trial}));
		
	  elseif (toneByBlockAndTrial(obsNum,block,trial)==2)

		% high-pitch tone was played in trial
		sumHighPitchTone = sumHighPitchTone + ...
			nansum(oknGain{obsNum,block,trial});

		validDataHighPitchTone =  validDataHighPitchTone + ...
			sum(isfinite(oknGain{obsNum,block,trial}));
		
	  else
		
		% no tone was played in trial
		sumNoTone = sumNoTone + ...
			nansum(oknGain{obsNum,block,trial});
		
		validDataNoTone =  validDataNoTone + ...
			sum(isfinite(oknGain{obsNum,block,trial}));
		
		
		
	  end % if
	  
	end % trial
	
  end % block
	
  % collect data within observer, compute gain measure
  gainLowPitchTone(obsNum) = sumLowPitchTone/validDataLowPitchTone;
  gainHighPitchTone(obsNum) = sumHighPitchTone/validDataHighPitchTone;
  gainNoTone(obsNum) = sumNoTone/validDataNoTone;

	
end % obsNum
  
% so far, the data are sorted by tones, now assign them to the grating
% directions that have been coupled with the respective tone during
% induction (no-tone condition remains unaffected)

gainLeftAssociated = nan(1,16);
gainRightAssociated = nan(1,16);

for obsNum=1:16

  if (lowPitchDirectionByObserver(obsNum)==-1)&(highPitchDirectionByObserver(obsNum)==+1)
  
	gainLeftAssociated(obsNum)  = gainLowPitchTone(obsNum);
	gainRightAssociated(obsNum) = gainHighPitchTone(obsNum);
	
	
  elseif (lowPitchDirectionByObserver(obsNum)==1)&(highPitchDirectionByObserver(obsNum)==-1)

	gainLeftAssociated(obsNum)  = gainHighPitchTone(obsNum);
	gainRightAssociated(obsNum) = gainLowPitchTone(obsNum);
	
  else
	error('conidtions are inconsistent');
  end
	
end

figure(3);
clf;
bar([1:3],mean([gainLeftAssociated;gainNoTone;gainRightAssociated]'));
hold on;
errorbar([1:3],...
	mean([gainLeftAssociated;gainNoTone;gainRightAssociated]'),...
	std([gainLeftAssociated;gainNoTone;gainRightAssociated]')/sqrt(16),...
	'k.');
ylim([-0.15 0.15]);
ylabel('gain');
xlabel('condition');
set(gca,'XTick',[1:3]);
set(gca,'XTickLabel',{'tone assoc. left','no tone','tone assoc. right'});
  
