0

The code below will not work because the program won't know if the trial number we are on is divisible by 20. Basically, I want to check if the trial = 20, 40, 60, etc. and if so, have the subject take a break if they wish.

numTrials = 345;

for trial = 1:ntrials

% Take a break every 20 trials, subject can press space key to move on
 if mod(trial, 20) == 0
     breakText = ['Take a break or press the spacebar to continue'];
     tic
     while doc < 30 && ~keyPress
         DrawFormattedText(window, breakText, 'center', 'center', black)
         Screen('Flip', window);
         if (keyCode(spaceKey) == 1)
             break;
         end
     end

end

Thanks in advance!

collegesista
  • 87
  • 3
  • 12
  • 1
    Why doesn't it work? Looks like it checks if a number is divisible by 20 just fine. – Suever Jul 14 '16 at 20:05
  • The problem is I want to do something like: if trial = mod(trial,20) == 0, but I can't use the equals operator like this. I need to check is the trial we are on is divisible by 20. – collegesista Jul 14 '16 at 20:07
  • You're already doing that with `mod(trial, 20) == 0` – Suever Jul 14 '16 at 20:10
  • Okay thank you. I wasn't sure if you had to set trial equal to something to ensure that it would do that. – collegesista Jul 14 '16 at 20:14

1 Answers1

1

You can use either mod or rem to determine if a number is divisible by another. For positive numbers, they both return the remainder after dividing two numbers. If a number is perfectly divisible by another, the remainder will be zero.

is_divisible_by_20 = rem(number, 20) == 0

This will evaluate to true for numbers which are perfectly divisible by 20 so you can put it in place of the condition on the if statement.

if rem(number, 20) == 0
    % Take a break
end
Suever
  • 64,497
  • 14
  • 82
  • 101