How to close a figure used for keypress function?

19 ビュー (過去 30 日間)
Brezelbayer
Brezelbayer 2023 年 2 月 7 日
コメント済み: Brezelbayer 2023 年 2 月 7 日
Hi,
I'm using the KeyPressFnc to capture user-feedback in a small test. The use of KeyPressFnc requires to open a (dummy) figure. To complete the test (aka run the code clean from top to bottom) I have to close the figue. So far I have not found a way to close the figure after the user feedback was captured.
How do I close the figure (correctly), so I reach the end of the test and get my 'End of Test' statement?
function parent % outer function
disp('This is the outer function')
hkey = figure;
set(hkey,'KeyPressFcn',@UserFeedback);
disp('Make your choice: Press left or right arrow key')
waitfor(hkey);
UserFeedback
disp('End of Test')
function UserFeedback(~,evnt) % inner function
if strcmpi(evnt.Key,'leftarrow')
disp('You chose option 1: leftarrow key.')
close(hkey)
elseif strcmpi(evnt.Key,'rightarrow')
disp('You chose option 2: rightarrow key.')
close(hkey)
else
disp('Invalid Key, please press left or right arrow key for evaluation.')
return
end
disp('This was the inner function')
end % inner function
end % outer function
  3 件のコメント
Jan
Jan 2023 年 2 月 7 日
@Brezelbayer: What is the problem with the shown code? Closing hkey looks correct, if hkey is a shared variable in a nested function. I'd prefer delete(hkey) but this might be a question of taste.
As W.J. said, it is unclear why you call UserFeedback() again.
Brezelbayer
Brezelbayer 2023 年 2 月 7 日
Thanks! Deleting the call UserFeedback() solved the problem.
I was taught that if I have multiple nested functions the code looks more structured, if I call the functions first and define the functions at the end (see code below). Since in my case calling the function first was not purposeful, I guess I was simply mixing up something, because I'm still a Matlab beginner.
function outer_1()
% some initialization code
x = 2;
y = 4;
disp(sprintf('x=%d,y=%d',x,y));
% call inner_1:
inner_1()
% a nested function
function inner_1()
disp('inner_1');
end
% another nested function
function inner_2()
disp('inner_2');
end
end

サインインしてコメントする。

採用された回答

Benjamin Kraus
Benjamin Kraus 2023 年 2 月 7 日
編集済み: Benjamin Kraus 2023 年 2 月 7 日
As @W. J. stated, your code seems to run fine if you just remove the call to UserFeedback from after the call to waitfor.
Alternatively, have you tried the input function? It won't work if you need to capture left arrow and right arrow, but it does allow you to capture user input without needing to create a separate figure, or deal with KeyPressFcn or callbacks.
If you stick with your approach, you can clean-up your code a little by using the first input to UserFeedback, like this:
function askForUserFeedback % outer function
disp('This is the outer function')
hkey = figure;
set(hkey,'KeyPressFcn',@UserFeedback);
disp('Make your choice: Press left or right arrow key')
waitfor(hkey);
disp('End of Test')
function UserFeedback(fig,evnt) % inner function
if strcmpi(evnt.Key,'leftarrow')
disp('You chose option 1: leftarrow key.')
close(fig)
elseif strcmpi(evnt.Key,'rightarrow')
disp('You chose option 2: rightarrow key.')
close(fig)
else
disp('Invalid Key, please press left or right arrow key for evaluation.')
return
end
disp('This was the inner function')
end % inner function
end % outer function

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by