首页 > 解决方案 > How to move to next iteration in for loop in Matlab

问题描述

I get users to do the following to adjust the luminance of grey squares through Psychtoolbox (allowing both big and small changes and registering these values).

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

I now would like to put this in a for loop. Ideally, the user would move to the next iteration by pressing a key or the mouse button.

I am somehow stuck. Should I use a continue function?

标签: matlabfor-loopbuttoniterationcontinue

解决方案


如果我理解正确,您希望在 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。您不需要在任何明确的停止条件下进行编程。话虽如此,这有点像一个 hacky 解决方案,它会中止正在运行的代码的每一部分。显式退出条件允许您更优雅地处理退出(例如关闭文件、写入日志、显示消息等)。还; 您应该知道,如果您的用户是恶意的,他们可能会这样做(除非 Psychtoolbox 可以防止我怀疑的那些)。


推荐阅读