function multiComp(sesDat)

% multiple comparison test on several 1D scale
% see readMultiComp for a description of specifications

UIControl_FontSize_bak = get(0, 'DefaultUIControlFontSize');
set(0, 'DefaultUIControlFontSize', 20);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% copy useful variables
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

reREAD=sesDat.reREAD;
noTst=sesDat.noTst;
nbTst=sesDat.nbTst;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% read the test file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
test=readMultiComp(sesDat.tstDat{noTst}.tstFile);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set the test screen
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

hf=figure;	
set(hf,'units','normalized');
set(hf,'position',[0.1 .15 .80 1.0]); % TODO make automatic % [left, bottom, width, height]
set(hf,'closereq',[]);
set(hf,'numbertitle','off');
set(hf,'menubar','none');
if (nbTst-noTst)/2 > 1
    titlestr = sprintf('%d screens left',(nbTst-noTst)/2);
else if (nbTst-noTst) > 1
        titlestr = 'One screen left after this';
    else 
        titlestr = 'Last screen';
    end
end
set(hf,'name',titlestr);
set(gca,'position',[0 0 1 1]);
axis([0 1 0 1]);
set(gca,'yticklabel',[]);
set(gca,'xticklabel',[]);
grid off;
hp=gca;

set(hp,'color',[.7 .7 .7]);	% gray background

% Figure-event properties
set(hf,'windowbuttonmotionfcn',sprintf('MmoveW(%d)',hf));
set(hf,'keypressfcn',sprintf('TpushKey(%d)',hf));

% Define Scales and SonIcons

nbScale=length(test.scale);
dY=1/(nbScale+3);	% vertical space for each scale+text (used to be +1, now +3)
dX=1;
dyy=dY/2;		% vertical space for scale and text
dxx=1/30;		% vertical margins of the scale
small=dyy/10;

% create sonicons
nbSnd=test.nbComb;

% RANDOM POSITIONS and ALREADY PLAYED
if reREAD && isfield(sesDat.tstDat{noTst}, 'rsl')
    permVec = sesDat.tstDat{noTst}.rsl.permVec;
    playVec = sesDat.tstDat{noTst}.rsl.playVec;
else
    permVec = randperm(nbSnd);
    playVec = zeros(nbSnd,1);
end

fidInd=['responses/' sesDat.id,'_',sesDat.sesScript]; % print permutation in response log
fid=fopen(fidInd,'a');
fprintf(fid,'\n%s\n', 'Random permutation mapping (comments): ');
for noSnd = 1: nbSnd
   fprintf(fid,'%d  ', permVec(noSnd));
end
fprintf(fid,'\n%s\n', ' ');


for noScale=1:nbScale
   
   y=1-noScale*dY;	% bottom line
   dyt=dyy; % was 3/2*
   %display text
   h='horizontalAlignment';
   v='verticalAlignment';
   text(.5,y+dyt,test.scale(noScale).name,h,'center',v,'bottom', 'FontSize', 24);	% Scale name
   % Level names
   text(0+small,y+dyt,test.scale(noScale).level{1},h,'left',v,'bottom', 'FontSize', 24); % MINIMUM
   text(1-small,y+dyt,test.scale(noScale).level{3},h,'right',v,'bottom', 'FontSize', 24); % MAXIMUM
   
   % Display 1D rules
   y0=y;
   y1=y+dyy;
   hold on;
   fill([dxx dxx 1-dxx 1-dxx],[y1 y0 y0 y1],[.8 .8 .8]);
   
   
   for noSnd=1:nbSnd
       
      dl=(1-2*dxx)/5; %BDM used to be /nbsnd
      xI=dl*noSnd/2;
      dxI=dl/10;		% width of a sonicon
      posIc=[xI y+small dxI dyy-2*small];
      
      % position the icons on the default position
      % check if the individual data contains already some past results
      % 
      if reREAD && isfield(sesDat.tstDat{noTst}, 'rsl')
         posComb=sesDat.tstDat{noTst}.rsl.mat(noScale,noSnd);
      else
         posComb=test.posComb(noSnd,noScale);
         fidInd=['responses/' sesDat.id,'_',sesDat.sesScript]; % print initial placing
         fid=fopen(fidInd,'a');
         fprintf(fid,'%f  ', test.posComb(noSnd,noScale));
      end
      
      % Calculate Icon position relative to the screen units
      minx=test.scale(noScale).minn;
      maxx=test.scale(noScale).maxx;
      a=(1-2*dxx)/(maxx-minx);
      b=dxx-a*minx;
      posIc(1)=posComb*a+b-dxI/2;
      
      % set handles      
      hIc=uicontrol('style','text','string',num2str(permVec(noSnd)),'units','normalized','position',posIc); % changed by Brecht: randperm
      set(hIc,'buttondownfcn',sprintf('MpushIc(%d)',hf));
      set(hIc,'backgroundcolor', [.6 .9 .6]); % set colour to green
      
      %%%%%%%%%%%%%%%%%%% ENABLING
      set(hIc,'enable','inactive');
      set(hIc,'tag','sonicon');
      
      sndId=test.comb(noSnd);
      set(hIc,'userdata',sndId);
      hIcon(noScale,noSnd)=hIc;
      
   end
   fprintf(fid,'%s\n', ' ');
   %shIc= set(hIc); %??? what is this here for? suppress output. 

   % set the un-movable references
   nbRef=length(test.sndRef);
   for noRef=1:nbRef
      sndRef=test.sndRef(noRef);
      %%%%%%%%%%%%%%%%%%%%%% ENABLING...
      set(hIcon(noScale,sndRef),'enable','off');		%% for MAC! may be 'off' for PC??
      set(hIcon(noScale,sndRef),'string','ref');
   end
   
   
end


% create Comments 
hcom = struct([]); % create empty struct for comments
if reREAD && isfield(sesDat.tstDat{noTst}, 'rsl')
    for noSnd = 1:nbSnd % a comment per sample
        posCom=[dxx 6*dY/(nbSnd+2)+3*dY/(nbSnd+2)*(nbSnd - noSnd) 10/15 3*dY/(nbSnd+2)-small]; % [left, bottom, width, height]
        commentstr = sesDat.tstDat{noTst}.rsl.hcom{noSnd};
        hcom{noSnd} = uicontrol('style','edit','max',2,'string',commentstr, ...
            'units','normalized','position',posCom, ...
            'HorizontalAlignment', 'left');
    end
    
    posCom=[dxx small 10/15 6*dY/(nbSnd+2)-small]; % general comments
    commentstr = sesDat.tstDat{noTst}.rsl.gcom;
    gcom=uicontrol('style','edit','max',2,'string',commentstr, ...
        'units','normalized','position',posCom, 'HorizontalAlignment', 'left');
    
    
else % recall comments
    for noSnd = 1:nbSnd % a comment per sample
        posCom=[dxx 6*dY/(nbSnd+2)+3*dY/(nbSnd+2)*(nbSnd - noSnd) 10/15 3*dY/(nbSnd+2)-small]; % [left, bottom, width, height]
        commentstr = sprintf('%d: ', noSnd);
        hcom{noSnd} = uicontrol('style','edit','max',2,'string',commentstr, ... 
                                'units','normalized','position',posCom, ...
                                'HorizontalAlignment', 'left');
    end

    posCom=[dxx small 10/15 6*dY/(nbSnd+2)-small]; % general comments
    commentstr = sprintf('%s: ', 'General comments');
    gcom=uicontrol('style','edit','max',2,'string',commentstr, ...
        'units','normalized','position',posCom, 'HorizontalAlignment', 'left');
    
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% "Stop audio" button
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

posFin=[11/15 3*dY/2 3/15 3*(dY-small)/2]; % [left, bottom, width, height]
hFin=uicontrol('style','pushbutton','string','Stop audio','units','normalized','position',posFin);
set(hFin,'callback',sprintf('MpushIc(%d)',hf));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% "Finished" button
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

posFin=[11/15 small 3/15 3*(dY-small)/2]; % [left, bottom, width, height]
hFin=uicontrol('style','pushbutton','string','Finished','units','normalized','position',posFin);
set(hFin,'callback',sprintf('endMultiComp(%d)',hf)); 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Test timer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dat.t0=clock;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Send useful datas
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dat.dxx     = dxx; %?
dat.dxI     = dxI; %?
dat.hf      = hf;
dat.hcom    = hcom; % individual comments
dat.gcom    = gcom; % general comments
dat.hIcon   = hIcon;
dat.drag    = 0;
dat.test    = test;
dat.sesDat  = sesDat;
dat.playVec = playVec;
dat.permVec = permVec;
dat.nbScale = nbScale; 
dat.nbSnd   = nbSnd; 
set(hf,'userdata',dat)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% clean up
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;