Collect response value with two for loops nested
Show older comments
Hello everybody 'I'm new and this is my first approach to matlab, I have a matrix problem; maybe it is trivial but I have tried to search for it and couldn't find anything useful. This is a simplified version of my code. I used psychtoolbox to present visual stimuli and i want that, for each block, it shows the same number of trials in the right side of the screen and then in the left one. For each side I need it to record the response (the key pressed, the reaction time and other information). I was able to concatenate in horizontal the two matrix "left and right", but when the program saves my results it only keeps the last "block" of results( when you run it creates a response matrix 5x4 and not a 20x4 one). Can someone please help me to concatenate in vertical the results in a single matrix? Thank you in advance I hope I made myself clear
Cheers
numBlocks=4;
dx=2;
sx=1;%1=dx 0=sx
side=repmat( [ dx sx],numBlocks, 1);%1=dx 0=sx
numTrials=5;
responsesDx= zeros(numTrials,2);
responsesSx= zeros(numTrials,2);
responses=zeros(numTrials*numBlocks,4);
for i=1:numBlocks %how many time it has to repeat the loop
for j=1:size(side,2)
if j==side(sx)
for j=1:numTrials
key_pressed= 3;
reaction_time=5;
responsesDx(j,1)= key_pressed;
responsesDx(j,2)= reaction_time;
end
else
for j=1:numTrials
key_prressed= 4;
reaction_time=6;
responsesSx(j,1)= key_pressed;
responsesSx(j,2)= reaction_time;
end
end
responses=[ responsesDx responsesSx];
end
end
2 Comments
Andy L
on 11 Aug 2014
First of all I noticed that in your second loop key_pressed is spelt key_prressed - is this ok? Also Have you tried sticking a keyboard into your loop to test why it isn't behaving as you expect? This pauses the program each time it comes to that line, allowing you to see what the values are on each iteration and help with your debug. The program resumes execution when you type continue in the workspace.
alice
on 11 Aug 2014
Accepted Answer
More Answers (1)
Iain
on 11 Aug 2014
Change:
responsesDx= zeros(numTrials,2);
responsesSx= zeros(numTrials,2);
responses=zeros(numTrials*numBlocks,4);
responsesDx(j,1)= key_pressed;
responsesDx(j,2)= reaction_time;
(and the like)
to
responsesDx= zeros(numBlocks,numTrials,2);
responsesSx= zeros(numBlocks,numTrials,2);
responses=zeros(numBlocks,numTrials,4);
responsesDx(i,j,1)= key_pressed;
responsesDx(i,j,2)= reaction_time;
(and the like)
Categories
Find more on Timing and presenting 2D and 3D stimuli 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!