I want to update my 'for loop'

6 次查看(过去 30 天)
Hyerin Park
Hyerin Park 2020-9-28
编辑: Rohit Pappu 2020-10-28
Dear community.
I want to update my 'for loop' to get a full valid response.
for example, I want to get 5 'Space response time' regardless of some Invalid response(that didn't push space key at response phase)
so i put 'Invalid' variable to update 'TrialN', but it didn't work.
Here is my sample code.
Thanks a lot in advance for any idea...
trialtest = 5;
invalid = 0;
trialN = trialtest + invalid
spaceTime = 1.5
spaceKey = KbName('space');
tflip = [];
for T = 1: trialN
Screen('DrawLine',w, 0, cx-10, cy, cx+10, cy, 3);
Screen('DrawLine',w, 0, cx, cy-10, cx, cy+10, 3);
Screen('Flip',w);
WaitSecs(1); %초 생각해봐야 함
Screen('FrameOval',w,0,[cx-40,cy-40,cx+40,cy+40],4);
fixOnset = Screen('Flip',w,tflip);
[space spaceRT] = KeyResponse(spaceTime,0,spaceKey); % wait key to continue
mspaceRT = spaceRT*1000;
if space ==1
Screen('DrawLine',w, 0, cx-10, cy, cx+10, cy, 3);
Screen('DrawLine',w, 0, cx, cy-10, cx, cy+10, 3);
Screen('Flip',w);
DrawTextUni(w,['GOOD'],'center',0,-rect(4)*.3);
elseif space == 999
Screen('DrawLine',w, 0, cx-10, cy, cx+10, cy, 3);
Screen('DrawLine',w, 0, cx, cy-10, cx, cy+10, 3);
Screen('Flip',w);
DrawTextUni(w,['BAD'],'center',0,-rect(4)*.3);
invalid = invalid +1
end
trialN = trialtest + invalid % I put this code for update, but it doesn't work T.T
end

回答(1 个)

Rohit Pappu
Rohit Pappu 2020-10-28
编辑:Rohit Pappu 2020-10-28
For loop does not support dynamic update of its conditions. A better alternative would be to use while loop instead
% Initialize t
t = 1;
while(t<=trialN)
% Block of code to be executed trialN times
t+=1; %Increment t
end

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by