Is there a way to continue operation during input()?

조회 수: 15 (최근 30일)
Bohan Yao
Bohan Yao 2019년 8월 16일
답변: per isakson 2019년 8월 18일
So what I'm essentially trying to do is, I have a switch statement in a while loop. Using an input() function, MATLAB will take the user input and put it through the switch cases. Pretty simple. But now, I want to modify it such that if MATLAB does not receive a command during input() for X number of seconds, it will continue the code, instead of waiting for user input. Ineveitably, it will go to the default switch case becuase a null input will not match any of the cases, and then the while loop will take me back to input(). So is there a way to "unpause" the code during input() automatically without me having to press ENTER?
  댓글 수: 2
Stephen23
Stephen23 2019년 8월 16일
편집: Stephen23 2019년 8월 18일
Your best choice is to write a GUI: a well-designed GUI is anyway a much better user-interface than input.
Bohan Yao
Bohan Yao 2019년 8월 16일
I agree, however, I am working with someone else's code, which they have already developed extensively. Implmenting a GUI would require a lot more work than is worth it. Is there another way to do this with just input()? If there truly is no other way to do it, then I guess I have no choiec but to create a GUI.

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2019년 8월 18일
https://www.mathworks.com/matlabcentral/fileexchange/8297-getkeywait
The below two mostly rely on Psychtoolbox
https://www.mathworks.com/matlabcentral/answers/310311-how-to-get-psychtoolbox-to-wait-for-keypress-but-move-on-if-it-hasn-t-recieved-one-in-a-set-time
https://www.mathworks.com/matlabcentral/answers/143088-real-time-detect-keypress

per isakson
per isakson 2019년 8월 18일
Something like
%% a script named cssm.m
while true
user_input = waitinput( 'prompt: ', 5, 's' );
if isnan( user_input )
user_input = 'default';
disp(' ')
end
switch user_input
case 'default'
disp('default')
case 'poi'
disp('poi')
case 'abc'
disp('abc')
case 'quit'
disp('good bye')
break
otherwise
error('failure')
end
end
might do the trick. That is to say waitinput by Grzegorz Knor does the trick.
And a little test
>> cssm
prompt:
default
prompt:
default
prompt: poi
poi
prompt:
default
prompt: quit
good bye

카테고리

Help CenterFile Exchange에서 Timing and presenting 2D and 3D stimuli에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by