function endMultiComp(hf) % called when 'Finished' clicked

playSound([]); % stop all sound

dat=get(hf,'userdata');

if(sum(dat.playVec) <= size(dat.sesDat.cuSndList,2)) % less than: allow for missing files... 
    x = mat2str(dat.permVec(find(dat.playVec==0)));
    errordlg(['Please continue until all sounds are assessed: you haven''t listened to ' x ' yet.'], 'Not all sounds played!');
    return;
end

% are you sure?
choice = questdlg('Are you sure you are finished and want to submit your response for this round?','Finish and submit','No','Yes','No');
if strcmp(choice, 'No')
    return; 
end

% TODO: replace some of the following by savemultiComp

sesDat=dat.sesDat;
test=dat.test;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Collect the results into rsl
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rsl.gcom=get(dat.gcom,'string'); % general comments
rsl.hcom=get(struct([]), 'struct'); % empty array of individual comments
rsl.permVec=dat.permVec;
rsl.playVec=dat.playVec;

nbSnd=test.nbComb;
nbScale=length(test.scale);

for noScale=1:nbScale
   for noSnd=1:nbSnd
      rsl.hcom{noSnd}=get(dat.hcom{noSnd}, 'string'); % individual comments
      posIc=get(dat.hIcon(noScale,noSnd),'position');
      scale=test.scale(noScale);
      posx=posIc(1);
      dxx=dat.dxx;
      dxI=dat.dxI;      
      minx=scale.minn;
      maxx=scale.maxx;
      stepp=scale.stepp;      
      a=(maxx-minx)/(1-2*dxx);
      b=minx-a*dxx;
      nx=a*(posx+dxI/2)+b;
      rsl.mat(noScale,noSnd)=nx;
   end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Timer 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rsl.duration=etime(clock,dat.t0);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% generate a small report file 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fid=reportTest(sesDat,rsl,'MULTIPLE COMPARISON 1D');

% scales
fprintf(fid,'%%%%%%%%%%%% scales ');eol(fid);eol(fid);
for noScale=1:nbScale
   fprintf(fid,'Scale #%d -- %s',noScale,test.scale(noScale).name);eol(fid);
end

% matrix results
for noScale=1:nbScale
   fprintf(fid,'SCALE #%d: ',noScale);
   for noSnd=1:nbSnd
       fprintf(fid,'%2.2f \t',rsl.mat(noScale,noSnd));
    end
eol(fid);
end

% COMMENTS
fprintf(fid,'');eol(fid);
fprintf(fid,'%%%%%%%%%%%% COMMENTS');eol(fid);

for index = 1:nbSnd
    com = rsl.hcom{dat.permVec(index)}; % individual comments
    fprintf(fid,'Sound %d, marker %s\n',index,com);
end

fprintf(fid,'%s\n',rsl.gcom); % print general comments


eol(fid);eol(fid);
fclose(fid);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Save it
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dat=get(hf,'userdata');
saveTest(dat,rsl,'multiComp');