How can i create an if statement based on multiple conditions?
Show older comments
I am trying to write a code for a visuomotor experiment. I have two blocks of trials: block 1 is a control(prime) block of trials, block 2 is a masked block of trials. I have written in all of my targets to appear in random order, like so:
if trials(block_type(block_counter),trial_counter) == 1
size=1;
space=1;
location=1;
Screen('DrawTexture', w1, p1); % display preview 1 for the appropriate target
elseif trials(block_type(block_counter),trial_counter)== 2
size=1;
space=1;
location=2;
Screen('DrawTexture', w1, p2); % display preview 1 for the appropriate target
elseif trials(block_type(block_counter),trial_counter)== 3
size=2;
space=1;
location=1;
Screen('DrawTexture', w1, p3); % display preview 1 for the appropriate target
elseif trials(block_type(block_counter),trial_counter)== 4
size=2;
space=1;
location=2;
Screen('DrawTexture', w1, p4); % display preview 1 for the appropriate target
elseif trials(block_type(block_counter),trial_counter)== 5
size=3;
space=1;
location=1;
Screen('DrawTexture', w1, p5); % display preview 1 for the appropriate target
elseif trials(block_type(block_counter),trial_counter)== 6
size=3;
space=1;
location=2;
Screen('DrawTexture', w1, p6); % display preview 1 for the appropriate target
etc....
There are 5 different target sizes and they appear at two different eccentricities in either left or right space. In block 2, I need to present a 4 dot mask (separate jpg image) surrounding the target immediately following the presentation of the target image, however since the target images are being presented randomly, i need to make sure the appropriate mask is presented for the corresponding eccentricity.
Basically, if matlab presents targets 1, 3, 5, 7, or 9, i need to present mask 1; if targets 2, 4, 6, 8, or 10 appear, i need to present mask 2; if targets 11, 13, 15, 17, or 19 appear, i need mask 3; if targets 12, 14, 16, 18, or 20 appear, i need mask 4;
I am thinking of something like this, however i am not sure if it will work or if this is proper. Does anyone have any feedback on how i can accomplish this?
if block_type(block_counter) == 2
if trials(block_type(block_counter),trial_counter) == 1, 3, 5, 7, 8, 9;
Screen('DrawTexture', w1, mask1); % mask left near
elseif trials(block_type(block_counter),trial_counter) == 2, 4, 6, 8, 10;
Screen('DrawTexture', w1, mask2); % mask left far
elseif trials(block_type(block_counter),trial_counter) == 11, 13, 15, 17, 19;
Screen('DrawTexture', w1, mask4); % mask right far
elseif trials(block_type(block_counter),trial_counter) == 12, 14, 16, 18, 20;
Screen('DrawTexture', w1, mask3); % mask right near
end
Thank you!!
2 Comments
Most likely this code would be a lot simpler without numbered variables, because then simple indexing could be used to access the values. For example:
M = [mask1,mask2,mask3..]; % pick the order to suit
tmp = trials(block_type(block_counter),trial_counter)
idx = 1+(tmp>10)+mod(tmp,2);
Screen('DrawTexture', w1, M(idx))
No if's required at all.
dpb
on 9 Feb 2017
Concur fully with Stephen's recommendation; was getting ready to write essentially the same thing before reading his comment... :)
I'll simply now note that
if trials(index) == 1, 3, 5, 7, 8, 9
is NOT valid Matlab syntax as you guessed; while
if trials(index) == [1:2:7 9]
is allowable syntax it would not do what you would hope in this case. (Look up if and read the doc carefully to see why).
While I'd follow Stephen's lead and write dynamic state machine-like decision logic here as well, I will just note that if you're adamant on implementing static branching,
switch(trial)
case {1, 3, 5, 7, 8, 9}
...
case {next set}
...
will work and is much more concise than the equivalent nested if...elseif...else...end.
Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!