【问题标题】:How to move to next iteration in for loop in Matlab如何在Matlab的for循环中移动到下一个迭代
【发布时间】:2018-08-13 17:44:54
【问题描述】:

我让用户执行以下操作以通过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 函数吗?

【问题讨论】:

  • 我很困惑。你没有for 循环。
  • 对不起:我想把它放在一个 for 循环中,我可以通过点击移动到下一个迭代。

标签: matlab for-loop button iteration continue


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

【讨论】:

    【解决方案2】:

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

    鼠标点击的答案: https://de.mathworks.com/help/matlab/ref/waitforbuttonpress.html;基于waitforbuttonpress

    您不需要for 循环,同样可以在while 循环中工作。

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

    【讨论】:

      猜你喜欢
      • 2013-03-18
      • 2019-11-29
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2023-03-25
      • 2021-05-15
      • 1970-01-01
      • 2015-05-09
      相关资源
      最近更新 更多