[HELP] required for exception handling during user input

Hi need help!
I need to capture user input using getkey(). However the user is only allowed to enter values between 1 and 9. If user presses something else it should wait (can give an error message too) for the user to press a valid key (1-9).
Code please.
Thanks in anticipation.

답변 (2개)

Sean de Wolski
Sean de Wolski 2012년 1월 12일
what is getkey()?
docsearch getkey
doesn't return any results. If you want the keypressfcn of a figure to perform this test, here is a small demo:
All in one file ans_keypressexample.m
function ans_keypressexample
figure('keypressfcn',@clicked); %make a figure
function clicked(~,evt)
key = str2double(evt.Key); %get the key
if isnan(key)||key==0 %is it between 1 and 9?
error('Booo!'); %no :(
else
disp('yay!!');
end

댓글 수: 1

GETKEY() waits for a keypress and returns the ASCII code, but dont think this is a standard function in matlab. It has been added separately to our matlab.
Thanks. I will try the above code.
Walter Roberson
Walter Roberson 2012년 1월 12일

0 개 추천

As you are using a File Exchange contribution, edit the source for your own purposes. http://www.mathworks.com/matlabcentral/fileexchange/7465-getkey

이 질문은 마감되었습니다.

질문:

2012년 1월 12일

마감:

2021년 8월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by