# Matrix dimensions will not agree for 'if' statement

**URL:** <https://psychtoolbox.discourse.group/t/matrix-dimensions-will-not-agree-for-if-statement/4185>\
**Category:** Programming Help\
**Tags:** macos\
**Created:** [January 14, 2022, 10:19am UTC](https://psychtoolbox.discourse.group/t/matrix-dimensions-will-not-agree-for-if-statement/4185 "2022-01-14T10:19:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cai](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@Cai](https://psychtoolbox.discourse.group/u/Cai)\
**Post date:** [January 14, 2022, 10:19am UTC](https://psychtoolbox.discourse.group/t/matrix-dimensions-will-not-agree-for-if-statement/4185/1 "2022-01-14T10:19:25Z")

</div>

Hi, I am attempting to run the following code, whereby, depending on the key that a candidate presses during a trial, a certain number is chosen randomly using the data stored in the ‘resultsMatrix’ structure. This number is then used to define the prize money the candidate receives.

```auto
choosing_trial_prize_1 = randi(4);
choosing_trial_prize_2 = randi(4);    
if resultsMatrix(choosing_trial_prize_1).Key2 == 'LeftArrow' % the cell resultsMatrix(choosing_trial_prize_1).Key2 contains the output of KbName(keyCode1), where keyCode1 is the arrow pressed. 
    winning_distribution_1 = resultsMatrix(choosing_trial_prize_1).Sleftnumbers;
elseif resultsMatrix(choosing_trial_prize_1).Key2 == 'RightArrow'
    winning_distribution_1 = resultsMatrix(choosing_trial_prize_1).Srightnumbers;
end
  
if resultsMatrix(choosing_trial_prize_2).Key2 == 'LeftArrow'
    winning_distribution_2 = resultsMatrix(choosing_trial_prize_2).Sleftnumbers;
elseif resultsMatrix(choosing_trial_prize_2).Key2 == 'RightArrow'      
    winning_distribution_2 = resultsMatrix(choosing_trial_prize_2).Srightnumbers;
end

prize_1 = randi(str2num(winning_distribution_1));
prize_2 = randi(str2num(winning_distribution_2));
overall_prize = prize_1 + prize_2;

line10 = 'Congratulations, you have won:';
line11 = num2str(overall_prize);

```

However, I keep getting the following error:

```auto
Matrix dimensions must agree.

Error in Perceptual_trial_test 
if resultsMatrix(choosing_trial_prize_2).Key2 == 'LeftArrow'

```

How may I resolve this issue? Any suggestions would be much appreciated. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![xiangrui](https://avatars.discourse-cdn.com/v4/letter/x/ec9cab/32.png) [@xiangrui](https://psychtoolbox.discourse.group/u/xiangrui)\
**Post date:** [January 14, 2022, 1:49pm UTC](https://psychtoolbox.discourse.group/t/matrix-dimensions-will-not-agree-for-if-statement/4185/2 "2022-01-14T13:49:00Z")

</div>

This sounds a matlab question, not a PTB one. You probably need to use “LeftArrow”, i.e., double quotation marks, or

```
if strcmp(resultsMatrix(choosing_trial_prize_2).Key2, 'LeftArrow')
```
