So, we now have a m-file that simulates a bunch of trials. Sometimes of course, we want to have experimental conditions or simply multiple blocks of trials. The easiest way to do this is by using nested loops.
What is a nested loop? It is a loop within a loop.
Let's think about this logically. We want to modify our current code a bit. Specifically, we want it so that on some blocks people have to do a simple reaction time task - push any button as soon as you see a target. In other blocks, we want to have them do the choice reaction time task from the previous tutorial. So, the loop structure is a loop that does blocks but within that a loop that does trials.
In general, it would look like this:
for block_counter = 1:number_of_blocks
for trial_counter = 1:number_of_trials
% do our trial stuff here
end
end
BUT... let's expand this a bit before I show you the code. Specifically, I am going to add one thing. We need to know what type of block it is before we start our trials so we know whether a given trial set is simple reaction time or choice reaction time. We typically use numbers as codes here - I will say a block_type of 1 is simple reaction time and a block_type of 2 is choice reaction time. So, that gets us:
for block_counter = 1:number_of_blocks
% determine block_type
for trial_counter = 1:number_of_trials
% if block_type == 1 do this, if block_type == 2 do this
end
end
Make sense? HERE is the code that does this experiment. Make sure you not only run the experiment but that you also play with the code! Change things, see what it does.
Things to do:
1. Change the stimuli size, location, and colour.
2. Change the response keys.
3. CHALLENGE. Add a third condition with three possible responses.
Now, one thing wrong with our code above is what happens if one of the conditions is never picked? Further, as a researcher you may want to counter balance your conditions to make sure there is an equal number of each condition - in this case simple and choice reaction time. We could do this one of two ways. One, we could manually specify the block_type as follows:
block_order = [1 2 1 2];
And then on each iteration of our block loop we just work through these in order. So, on loop one of block_counter we use 1, on loop two we use 2, on loop three we use 1 again, and so on. HERE is code that does this. Carefully examine the difference between this and the previous code I gave you.
Now, this gives you the ability to specify the block order or even randomize it by changing this for each participant. However, you can have MATLAB do this for you as well.
block_order = [1 1 2 2];
shuffle(block_order);
HERE is a version that does that.
There is quite a bit in this new code but it is almost a fully functional experiment ready to go. Make sure you carefully review the experimental code and understand each line before you move on!
What is a nested loop? It is a loop within a loop.
Let's think about this logically. We want to modify our current code a bit. Specifically, we want it so that on some blocks people have to do a simple reaction time task - push any button as soon as you see a target. In other blocks, we want to have them do the choice reaction time task from the previous tutorial. So, the loop structure is a loop that does blocks but within that a loop that does trials.
In general, it would look like this:
for block_counter = 1:number_of_blocks
for trial_counter = 1:number_of_trials
% do our trial stuff here
end
end
BUT... let's expand this a bit before I show you the code. Specifically, I am going to add one thing. We need to know what type of block it is before we start our trials so we know whether a given trial set is simple reaction time or choice reaction time. We typically use numbers as codes here - I will say a block_type of 1 is simple reaction time and a block_type of 2 is choice reaction time. So, that gets us:
for block_counter = 1:number_of_blocks
% determine block_type
for trial_counter = 1:number_of_trials
% if block_type == 1 do this, if block_type == 2 do this
end
end
Make sense? HERE is the code that does this experiment. Make sure you not only run the experiment but that you also play with the code! Change things, see what it does.
Things to do:
1. Change the stimuli size, location, and colour.
2. Change the response keys.
3. CHALLENGE. Add a third condition with three possible responses.
Now, one thing wrong with our code above is what happens if one of the conditions is never picked? Further, as a researcher you may want to counter balance your conditions to make sure there is an equal number of each condition - in this case simple and choice reaction time. We could do this one of two ways. One, we could manually specify the block_type as follows:
block_order = [1 2 1 2];
And then on each iteration of our block loop we just work through these in order. So, on loop one of block_counter we use 1, on loop two we use 2, on loop three we use 1 again, and so on. HERE is code that does this. Carefully examine the difference between this and the previous code I gave you.
Now, this gives you the ability to specify the block order or even randomize it by changing this for each participant. However, you can have MATLAB do this for you as well.
block_order = [1 1 2 2];
shuffle(block_order);
HERE is a version that does that.
There is quite a bit in this new code but it is almost a fully functional experiment ready to go. Make sure you carefully review the experimental code and understand each line before you move on!