Home Limit keyboard input to specific characters with while loop and get()
 I am trying to limit the scope of possible inputs on a figure to 'y' and 'n' in a while loop in Matlab. However, the code below does not work as it (to my understanding at least) should. answ = get(fig, 'CurrentCharacter'); while answ ~= 'n' & answ ~= 'y' answ = get(fig, 'CurrentCharacter'); end  How can I achieve the desired behavior? Only the first input seems to be caught by get. When I display the variable, the loop just goes on forever, even if the character in answ clearly is 'n' or 'y' and does not change with subsequent key presses. Additionally, sometimes the figure will disappear and the input will start getting caught by the terminal. Executing the code in a command window seems to yield the desired behavior: >> answ answ = n >> answ ~= 'n' & answ ~= 'y' ans = 0 answ = x >> answ ~= 'n' & answ ~= 'y' ans = 1