function [SQ, SO]=SQSO(mygui, probnum, daynum, task, path, numback, rounds)

% all the letters
letcode=textread('squares.txt', '%s','whitespace','\t');
soundcode=textread('sounds.txt', '%s','whitespace','\t');

datum=datestr(now, 30);   

% the numbers of trials between breaks
trials_tot=20+numback;

% the number of squares, % the number of sounds
% CHANGE TO 10!!
let_tot=8;
% press when there is supposed to be a press
hits=0;
hitsS=0;
% no press when supposed to
misses=0;
missesS=0;
% press when not supposed to press
falarms=0;
falarmsS=0;
% no press when not supposed to press
nopress=0;
nopressS=0;

% save the response times when same as 3 back
RTcount=1;
RTsame(RTcount)=0;
dist{:,:}=0;
distS{:,:}=0;
correct(:,:)=0;
correctS(:,:)=0;
pressS(:,:)=0;
pressSO(:,:)=0;

daytask=[num2str(numback) ' Back'];
mygui.output = uicontrol('BackgroundColor','k','ForegroundColor','w','Style','text','FontWeight','light','Units','normalized',...
        'Position',[0 0.4 1 0.2],'FontSize',35,'string',daytask);
pause(2); 
set(mygui.output,'String','','Visible','off');

% which number of the vector Mrand will be used
Mtrial=1;
Strial=1;
% the letter presented as a non-interference letter
Mrand=randperm(let_tot);
% the sounds presented as a non-interference sound
% 50 trials, of which 12.5% non-interference
Srand=randperm(let_tot);
for t=1:trials_tot
    ThreeB{t}=0;
end
for t=1:trials_tot
    ThreeS{t}=0;
end

% match: 50 randomized, odd numbers indicate the presentation of matching letter (50%)
% when randomized number is below 12.5 (12.5% of 50 trials)) BUT first 2 has to be even numbers
% because then matching letter cannot be presented,
% when randomized number above 12.5 (37.5% of 50 trials)), letter with
% interference presented
match=[zeros(1,numback) randperm(20)]; 
matchS=[zeros(1,numback) randperm(20)];

for trial=1:trials_tot
    % fixation
    mygui.output = uicontrol('BackgroundColor','k','ForegroundColor','w','Style','text','FontWeight','light','Units','normalized',...
        'Position',[0 0.45 1 0.2],'FontSize',60,'FontWeight','bold','string','.'); 
    pause(1.5); 
    set(mygui.output,'String','','Visible','off');
    
    if numback==2
        [ThreeB, dist, correct, Mtrial, pressS]=pres_2B(ThreeB, trial, match, letcode, Mrand, Mtrial, rounds, trials_tot, dist, correct, pressS);
        [ThreeS, distS, correctS, Strial, pressSO]=pres_2B(ThreeS, trial, matchS, soundcode, Srand, Strial, rounds, trials_tot, distS, correctS, pressSO);
    elseif numback==3
        [ThreeB, dist, correct, Mtrial, pressS]=pres_3B(ThreeB, trial, match, letcode, Mrand, Mtrial, rounds, trials_tot, dist, correct, pressS);
        [ThreeS, distS, correctS, Strial, pressSO]=pres_3B(ThreeS, trial, matchS, soundcode, Srand, Strial, rounds, trials_tot, distS, correctS, pressSO);
    else 
        [ThreeB, dist, correct, Mtrial, pressS]=pres_4B(ThreeB, trial, match, letcode, Mrand, Mtrial, rounds, trials_tot, dist, correct, pressS, numback);
        [ThreeS, distS, correctS, Strial, pressSO]=pres_4B(ThreeS, trial, matchS, soundcode, Srand, Strial, rounds, trials_tot, distS, correctS, pressSO, numback);
    end
    
    tic;
    % presentation of the square of trial
    Face1{trial,1}=ThreeB{trial};
    instrFile = Face1{trial,1};
    [im, map] = imread( instrFile ); 
    set(gcf,'ColorMap', map);
    image(im);
    axis off
    axis image
    pause(.05);
    
    y = wavread(ThreeS{trial});
    wavplay(y,41000);
    
    clf;
    answ=getkey(1);

    square{(rounds-1)*trials_tot+trial}=ThreeB{trial};
    letsound{(rounds-1)*trials_tot+trial}=ThreeS{trial};
    
    % if square is correct, press 1, if sound press 2, if both press 3
    if (ceil(match(trial)/2)==ceil((match(trial)+1)/2) || ceil(matchS(trial)/2)==ceil((matchS(trial)+1)/2)) ...
            && trial>numback && answ~=999
        RTsame(RTcount)=toc;
        RTall((rounds-1)*trials_tot+trial)= RTsame(RTcount);
        RTcount=RTcount+1;
    else 
        RTall((rounds-1)*trials_tot+trial)= toc;
    end
    answe{(rounds-1)*trials_tot+trial}=num2str(answ);
    if answ==999
            RTall((rounds-1)*trials_tot+trial)=0;
            answe{(rounds-1)*trials_tot+trial}=num2str(0);
    end
    
        % correct square
    if (correct((rounds-1)*trials_tot+trial)==1 && answ==49) || (correct((rounds-1)*trials_tot+trial)==1 && ... 
        answ==51) || ...
        (correct((rounds-1)*trials_tot+trial)==0 && (answ==50 || answ==999))
        coransw((rounds-1)*trials_tot+trial)=1;
        if correct((rounds-1)*trials_tot+trial)==1
            hits=hits+1;% response time for progress figure: press when supposed to
        else
            misses=misses+1;
        end
    else
        coransw((rounds-1)*trials_tot+trial)=0;
        if correct((rounds-1)*trials_tot+trial)==0
            falarms=falarms+1;
        else
            nopress=nopress+1;
        end
    end
    % correct sound
    if (correctS((rounds-1)*trials_tot+trial)==1 && answ==50) || ... 
        (correctS((rounds-1)*trials_tot+trial)==1 && answ==51) || ...
        (correctS((rounds-1)*trials_tot+trial)==0 && (answ==49 || answ==999))
        coranswS((rounds-1)*trials_tot+trial)=1;
        if correctS((rounds-1)*trials_tot+trial)==1
            hitsS=hitsS+1;% response time for progress figure: press when supposed to
        else
            missesS=missesS+1;
        end
    else
        coranswS((rounds-1)*trials_tot+trial)=0;
        if correctS((rounds-1)*trials_tot+trial)==0
            falarmsS=falarmsS+1;
        else
            nopressS=nopressS+1;
        end
    end
end

% no correct responses during the whole task
if RTcount==1
    RTcount=2;
end
datum_end=datestr(now, 30);
% data saving
savefile = fullfile(path,'datafolder',sprintf('%s%s%d%s%d%s%s',probnum{1},'D',daynum,'R',rounds,task,'.dat'));
fid = fopen(savefile, 'at');
% file for presenting progress
savefilePRO1 = fullfile(path,'datafolder',sprintf('%s%s%d%s%d%s',probnum{1},'D',daynum,'R',rounds,'progress.dat'));
fidPRO1 = fopen(savefilePRO1, 'at');
%savefilePRO2 = fullfile(path,'datafolder',sprintf('%s%s%d%s%d%s',probnum{1},'D',daynum,'R',rounds,'progress.dat'));
%fidPRO2 = fopen(savefilePRO2, 'at');
taskcode=['N' int2str(numback) 'B'];
for j=((rounds-1)*trials_tot+1):((rounds-1)*trials_tot+trials_tot)
    if j == (rounds-1)*trials_tot+1
        % ADD COLUMN WITH FOR PRESSES: EITHER OR, or BOTH? or none
        output1= [probnum{1} '\t' datum '\t' datum_end '\n' 'code' '\t' 'SQUARE' '\t\t' 'SQ: s/i/non-i' '\t' 'SQ: cor ans' '\t'...
            'SQ: accur' '\t' 'SOUND' '\t' 'SO: s/i/non-i' '\t' 'SO: cor ans' '\t'...
            'SO: accur' '\t' 'RT' '\t' 'key pressed' '\t' 'key:1,2,3' '\n'];
        fprintf(fid, output1);
    end
    if pressS(j)==1 && pressSO(j)==0
        presses(j)=1;
    elseif pressS(j)==0 && pressSO(j)==1
        presses(j)=2;
    elseif pressS(j)==1 && pressSO(j)==1
        presses(j)=3;
    elseif pressS(j)==0 && pressSO(j)==0
        presses(j)=0;
    else
        presses(j)=999;
    end
    if j<((rounds-1)*trials_tot+1)+(numback+1)
        codefor=[taskcode num2str(0)];
    else
        codefor=[taskcode num2str(j-numback)];
    end
    output2= [codefor '\t' square{j} '\t' dist{j} '\t' num2str(correct(j))...
        '\t' num2str(coransw(j)) '\t'];
    output3= [letsound{j} '\t' distS{j} '\t' num2str(correctS(j))...
        '\t' num2str(coranswS(j)) '\t' num2str(RTall(j)) '\t' answe{j} '\t' num2str(presses(j)) '\n']; 
    fprintf(fid,output2);
    fprintf(fid,output3);
end
% make it different for feedback: press when supposed to; press when not
% supposed to; no press when not supposed to; no press when supposed to
SQ=sum(coransw(:))-numback;
SO=sum(coranswS(:))-numback;
% both saved
outputPRO1= [num2str(SQ) '\t' num2str(SO) '\t' num2str(hits+hitsS) '\t' num2str(misses+missesS) ...
    '\t' num2str(falarms+falarmsS) '\t' num2str(nopress+nopressS) '\t' num2str(mean(RTsame(1:(RTcount-1)))) '\t' num2str(numback) '\n'];
fprintf(fidPRO1,outputPRO1);
fclose(fid);
fclose(fidPRO1);
