MATLAB 问题,为什么我的代码一次循环就结束了,需要6次


clc;
clear;

sr=44100;
gatedur=0.02;
pin=[100,300,500,700,900,1100];
gate=cos(linspace(pi,2*pi,sr*gatedur));%余弦变换
gate=(gate+1)/2;
offsetgate=fliplr(gate);

height=zeros(1,6);
time=zeros(1,6);
try
[CH,KU]=Screen('WindowSize', 0);
[M,N]=Screen('DisplaySize', 0);
INCH=sqrt(M^2+N^2)/2.54/10;
p1=deg2pix(1,INCH,CH,50,KU/CH);
KbName('UnifyKeyNames');
spaceKey= KbName('space');
escape= KbName('escape');
circleRadius = 30;
PsychDefaultSetup(2);
Screen('Preference', 'SkipSyncTests', 1); 
w=Screen('OpenWindow',0,0,[0,0,500,500]); 
circleColor = [0 0 255];
n=0;
i=1;
while n<6
sui=randi(6);
f=MakeBeep(pin(sui),0.2,44100);
sustain=ones(1,(length(f)-2*length(gate)));
envelope=[gate,sustain,offsetgate];
smoothed_tone=envelope.*f;
sound(smoothed_tone);
t0 = GetSecs;
exitFlag=false;
while ~exitFlag
[x, y, buttons] = GetMouse(w);
if any(buttons)
% Draw the circle at the clicked position
Rect=[250-circleRadius, y-circleRadius, 250+circleRadius, y+circleRadius];
Screen('FillOval',w, circleColor, Rect);
Screen('Flip', w);
end
[keyIsDown,seconds,keyCode]=KbCheck;
if keyIsDown
if keyCode(spaceKey)
height(1,i)=y;
time(1,i)=seconds-t0;
n=n+1;
break;
end
end
end
exitFlag=false;
continue; 
end
sca;
catch ME
Screen('Close', w)
rethrow(lasterror)
end

把n=n+1语句移到if语句内部试试看👀

if keyCode(spaceKey)
    height(1,i)=y;
    time(1,i)=seconds-t0;
    n=n+1;
    break;
end
n=n+1;