Home Limit keyboard input to specific characters with while loop and get()
Reply: 0

Limit keyboard input to specific characters with while loop and get()

user944
1#
user944 Published in May 27, 2018, 3:35 am

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
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.333207 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO