How do I break from a loop on keyboard input without plugins?

I have been trying to figure out for a while how to accomplish the following, but I don't want to use plugins. This is because the Psychtoolbox stopped working, and I don't want to fix it:
while 1
% perform task
% poll keyboard, do not stop, just check if a button is pressed
if button is pressed
break;
end
end
The faster the polling happens, the better.

 採用された回答

Josh
Josh 2024 年 5 月 4 日
編集済み: Josh 2024 年 5 月 10 日

0 投票

I've not implemented either of these solutions but seems to be on the right track...
...pretty sure the waitforbuttonpress function followed by a "break" gets you there

3 件のコメント

Josh
Josh 2024 年 5 月 4 日
might be able to incorporate uiwait and uiresume if you want to do a pause instead of break behavior
Salah Ad-Din Ahmed
Salah Ad-Din Ahmed 2024 年 5 月 10 日
I used this answer and it works: "You can create a waitbar or any other GUI which contains a button for breaking the loop. Then check a property inside the loop and break is the value chnages. This property could be the existence of the figure or the button as well as the UserData of the button or figure.
ttonHandle = uicontrol('Style', 'PushButton', ...
'String', 'Stop loop', ...
'Callback', 'delete(gcbf)');
for k = 1:1e6
disp(k)
if ~ishandle(ButtonHandle)
disp('Loop stopped by user');
break;
end
end
Josh
Josh 2024 年 5 月 10 日
very nice!

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

その他の回答 (0 件)

カテゴリ

質問済み:

2024 年 5 月 4 日

コメント済み:

2024 年 5 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by