如何在Matlab中的for循环中移至下一个迭代

时间:2018-08-13 17:44:55

标签: matlab for-loop button iteration continue

我让用户执行以下操作,以通过Psychtoolbox调整灰色方块的亮度(允许进行大大小小的更改并注册这些值)。

while exitDemo == false
    [keyIsDown,secs, keyCode] = KbCheck;

    if keyCode(escapeKey)
        exitDemo = true;
    elseif keyCode(more_lum_small)
        rectColor = rectColor + smallcolorchange;
    elseif keyCode(less_lum_small)
        rectColor = rectColor - smallcolorchange;
    elseif keyCode(more_lum_large)
        rectColor = rectColor + bigcolorchange;
    elseif keyCode(less_lum_large)
        rectColor = rectColor - bigcolorchange;
    end

    if keyCode(more_lum_small)
        colorcounter = colorcounter + 0.001;
    elseif keyCode(less_lum_small)
        colorcounter = colorcounter - 0.001;
    elseif keyCode(less_lum_large)
        colorcounter = colorcounter - 0.1;
    elseif keyCode(more_lum_large)
        colorcounter = colorcounter + 0.1;
    end

    centeredRect = CenterRectOnPointd(baseRect, squareX, squareY);
    centeredRect2 = CenterRectOnPointd(baseRect2, square2X, square2Y);
    banner_break = CenterRectOnPointd(banner, bannerX, bannerY);

    % Draw the rect to the screen
    Screen('FillRect', window, rectColor, centeredRect);
    Screen('FillRect', window, rect2Color, centeredRect2);
    Screen('FillRect', window, bannerColor, banner_break);

    % Flip to the screen
    vbl  = Screen('Flip', window, vbl + (waitframes - 0.5) * ifi);
end

我现在想将其放入for循环中。理想情况下,用户将通过按键或鼠标按钮移至下一个迭代。

我莫名其妙地被困住了。我应该使用continue函数吗?

2 个答案:

答案 0 :(得分:1)

如果我理解正确,那么您想在for循环中多次对用户运行这种类型的测试。我不想安装Psychtoolbox只是为了回答一个问题,所以我想我将通过3个问题测验来模拟自己的示例。您可以通过回答任何问题的q(退出)来停止上述测验。我认为这将是您的应用程序。

%% Initialise questions and answers
prompts = {...
    'What instrument did Sherlock Holmes play?';
    'How do we get rid of the pigeons form the roof?';
    'What did you bring me this time minion!?!'};
answers = {...
    'trumpet';
    'bazooka';
    'window'};
no_responses = {...
    'Hmm, interesting... "%s" you say...?\n';
    'Madness! "%s" will never work!\n';
    'Yes! Now that the "%s" is complete, people will tremble at my masters plan!\n'};
yes_responses = {...
    'Splendid! That''s correct\n';
    'Yes... This might work\n';
    'Nooo...!!! The light! It burns!\n'};
completion_message = 'Level up!';

%% Ask questions
exitDemo = false;
for j = 1:numel(no_responses)
    fprintf('--- Question %d ---\n',j);

    response = no_responses{j};
    prompt = sprintf('%s\n>',prompts{j});

    % Loop while has not gotten a correct answer yet
    gotCorrectAnswer = false;
    while ~gotCorrectAnswer
        answer = input(prompt,'s');
        answer = lower(answer);

        if strcmp(answer,'q') % Check for exit condition
            exitDemo = true;
            break
        elseif strcmp(answer,answers{j}) % Check for the correct answer
            fprintf(yes_responses{j});
            gotCorrectAnswer = true;
        else
            fprintf(no_responses{j},answer);
        end
    end

    % Check whether broke out of the for loop due to exit condition
    if exitDemo
        break
    end
end

if ~exitDemo
    fprintf(completion_message);
end

请注意,在执行代码时,通过按键盘上的Ctrl+C可以达到相同的效果。您可以例如停止像while true; pause(1); end这样的代码。您无需在任何明确的停止条件下进行编程。话虽如此,这是一个有点棘手的解决方案,它将中止正在运行的代码的每个部分。显式退出条件使您可以更优雅地处理退出(例如,关闭文件,写入日志,显示消息等)。也;您应该意识到,如果您的用户是恶意的,他们可能会这样做(除非Psychtoolbox可以防范我所怀疑的行为)。

答案 1 :(得分:0)

按键的答案: https://stackoverflow.com/a/9311250/5841680;基于input()

鼠标单击的

答案: https://de.mathworks.com/help/matlab/ref/waitforbuttonpress.html;基于waitforbuttonpress

您不需要for循环,而在while循环中也一样。

希望我确实理解了这个问题...