function [] = figure2

%
% function [] = figure2
%
% plots figure 2 of
% Intraindividual consistency between auditory and visual multistability
% by Einhäuser, W., da Silva, L.F.O., & Bendixen, A.
%
% requires the file stateSequences.mat to be in the same directory
%

% load sequences
load stateSequences xAud xVis xMix tAud tVis tMix;

% number of observers
nSubs = numel(xAud);

% number of transitions total
nSwTotAud = nan(1,nSubs);
nSwTotVis = nan(1,nSubs);
nSwTotMix = nan(1,nSubs);

% number of integrated states
nIntAud = nan(1,nSubs);
nIntVis = nan(1,nSubs);
nIntMix = nan(1,nSubs);

% number of segregated states
nSegAud = nan(1,nSubs);
nSegVis = nan(1,nSubs);
nSegMix = nan(1,nSubs);

for s = 1 : nSubs
  
  nSwTotAud(s)  = sum((xAud{s}>=1)&(xAud{s}<=3))-2;
  nSwTotVis(s)  = sum((xVis{s}>=1)&(xVis{s}<=3))-2;
  nSwTotMix(s)  = sum((xMix{s}>=1)&(xMix{s}<=3))-2;

  nIntAud(s)  = sum(xAud{s}==1);
  nIntVis(s)  = sum(xVis{s}==1);
  nIntMix(s)  = sum(xMix{s}==1);
  
  nSegAud(s)  = sum((xAud{s}>=2)&(xAud{s}<=3));
  nSegVis(s)  = sum((xVis{s}>=2)&(xVis{s}<=3));
  nSegMix(s)  = sum((xMix{s}>=2)&(xMix{s}<=3));
  
end

figure(2);
clf;

% the actual plot

subplot(3,3,1);
plot(nSwTotAud,nSwTotVis,'ko');
[r,p] = corr(nSwTotAud',nSwTotVis');
xlabel('# total transitions (A)','FontSize',6);
ylabel('# total transitions (V)','FontSize',6);
axis equal;
axis([0 250 0 250]);
title(sprintf('r(%d)=%5.2f; %s',nSubs-2,r,pForTitle(p)),'FontSize',6);
P = polyfit(nSwTotAud,nSwTotVis,1);
hold on;
plot([0:250], P(1)*[0:250]+P(2));

subplot(3,3,2);
plot(nSwTotMix,nSwTotVis,'ko');
[r,p] = corr(nSwTotMix',nSwTotVis');
xlabel('# total transitions (M)','FontSize',6);
ylabel('# total transitions (V)','FontSize',6);
axis equal;
axis([0 250 0 250]);
title(sprintf('r(%d)=%5.2f; %s',nSubs-2,r,pForTitle(p)),'FontSize',6);
P = polyfit(nSwTotMix,nSwTotVis,1);
hold on;
plot([0:250], P(1)*[0:250]+P(2));

subplot(3,3,3);
plot(nSwTotMix,nSwTotAud,'ko');
[r,p] = corr(nSwTotMix',nSwTotAud');
xlabel('# total transitions (M)','FontSize',6);
ylabel('# total transitions (A)','FontSize',6);
axis equal;
axis([0 250 0 250]);
title(sprintf('r(%d)=%5.2f; %s',nSubs-2,r,pForTitle(p)),'FontSize',6);
P = polyfit(nSwTotMix,nSwTotAud,1);
hold on;
plot([0:250], P(1)*[0:250]+P(2));


subplot(3,3,4);
plot(nIntAud,nIntVis,'ko');
[r,p] = corr(nIntAud',nIntVis');
xlabel('# valid states (A)','FontSize',6);
ylabel('# valid states (V)','FontSize',6);
axis equal;
axis([0 100 0 100]);
title(sprintf('r(%d)=%5.2f; %s',nSubs-2,r,pForTitle(p)),'FontSize',6);
P = polyfit(nIntAud,nIntVis,1);
hold on;
plot([0:100], P(1)*[0:100]+P(2));

subplot(3,3,5);
plot(nIntMix,nIntVis,'ko');
[r,p] = corr(nIntMix',nIntVis');
xlabel('# valid states (M)','FontSize',6);
ylabel('# valid states (V)','FontSize',6);
axis equal;
axis([0 100 0 100]);
title(sprintf('r(%d)=%5.2f; %s',nSubs-2,r,pForTitle(p)),'FontSize',6);
P = polyfit(nIntMix,nIntVis,1);
hold on;
plot([0:100], P(1)*[0:100]+P(2));

subplot(3,3,6);
plot(nIntMix,nIntAud,'ko');
[r,p] = corr(nIntMix',nIntAud');
xlabel('# valid states (M)','FontSize',6);
ylabel('# valid states (A)','FontSize',6);
axis equal;
axis([0 100 0 100]);
title(sprintf('r(%d)=%5.2f; %s',nSubs-2,r,pForTitle(p)),'FontSize',6);
P = polyfit(nIntMix,nIntAud,1);
hold on;
plot([0:100], P(1)*[0:100]+P(2));


subplot(3,3,7);
plot(nSegAud,nSegVis,'ko');
[r,p] = corr(nSegAud',nSegVis');
xlabel('# valid states (A)','FontSize',6);
ylabel('# valid states (V)','FontSize',6);
axis equal;
axis([0 200 0 200]);
title(sprintf('r(%d)=%5.2f; %s',nSubs-2,r,pForTitle(p)),'FontSize',6);
P = polyfit(nSegAud,nSegVis,1);
hold on;
plot([0:200], P(1)*[0:200]+P(2));

subplot(3,3,8);
plot(nSegMix,nSegVis,'ko');
[r,p] = corr(nSegMix',nSegVis');
xlabel('# valid states (M)','FontSize',6);
ylabel('# valid states (V)','FontSize',6);
axis equal;
axis([0 200 0 200]);
title(sprintf('r(%d)=%5.2f; %s',nSubs-2,r,pForTitle(p)),'FontSize',6);
P = polyfit(nSegMix,nSegVis,1);
hold on;
plot([0:200], P(1)*[0:200]+P(2));

subplot(3,3,9);
plot(nSegMix,nSegAud,'ko');
[r,p] = corr(nSegMix',nSegAud');
xlabel('# valid states (M)','FontSize',6);
ylabel('# valid states (A)','FontSize',6);
axis equal;
axis([0 200 0 200]);
title(sprintf('r(%d)=%5.2f; %s',nSubs-2,r,pForTitle(p)),'FontSize',6);
P = polyfit(nSegMix,nSegAud,1);
hold on;
plot([0:200], P(1)*[0:200]+P(2));


for k=1:9
  subplot(3,3,k);
  set(gca,'FontSize',6);
  if k<=3
	set(gca,'XTick',[0:50:250]);
	set(gca,'YTick',[0:50:250]);
	line([0 250],[0 250],'LineStyle',':');
  elseif k<=6
	set(gca,'XTick',[0:50:100]);
	set(gca,'YTick',[0:50:100]);
	line([0 100],[0 100],'LineStyle',':');
  else
	set(gca,'XTick',[0:50:200]);
	set(gca,'YTick',[0:50:200]);
	line([0 200],[0 200],'LineStyle',':');
  end
end


function [pString] = pForTitle(p)

% subfunction to format p<.001 approrpriately
if p<0.001
    pString='p<0.001';
else
    pString=sprintf('p=%6.3f',p);
end


