Press a key to stop the program?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
function funny_func()
while true
printf("hello world");
pause(1);
end
end
So basically I need to edit my code so that I can manually stop the code.
For example >>> funny_func() hello world hello world hello world
(Then I press "j" it has to be specially "j" in the keyboard") Then the program will stop. Anyone know how to do it? Thanks
채택된 답변
Amit
2014년 1월 28일
0 개 추천
I think you can use this function from fileexchange: http://www.mathworks.com/matlabcentral/fileexchange/8297-getkeywait
댓글 수: 9
keith lin
2014년 1월 28일
I don't want it to be time limited.
Amit
2014년 1월 28일
Hint: getkeywait(p) returns -1 if nokey is pressed. You can use that for your while loop.
And you're using pause(1). You can use that 1 sec for the user to press the key.
Amit
2014년 1월 28일
Also, printf is not a command in Matlab.
keith lin
2014년 1월 28일
nononoo. The pause(1) is there for a reason. I don't want to change that I want that pause in the program. I just want my program to pring "hello world" every one second, and stop whenever I press the letter "j".
That is what I am saying. getkeywait(p) waits for p seconds and if you enter a key, it will produce the key number which will be greater than 0. If you dont press the key in p secs, it will produce -1. Thus instead of pause(1), you can do getkeywait(1). They both are identical.
Look at the code below. This will wait 1 second between two consecutive 'hello world', given you dont press a key. You need to get that function to get this work though.
function funny_func()
while (getkeywait(1) < 0)
disp('hello world');
end
end
And if you want to modify this just for j, the ascii code for j is 106 and for J is 74. The code below will only break out when key j or J is pressed.
function funny_func()
ch = -1;
while ~(ch == 106 || ch == 74)
disp('hello world');
ch = getkeywait(1);
end
end
I want to keep the "pause(1);" The question is for part of the project I am working on, the pause(1) could be anything else, It could be
function funny_func()
while true
printf("hello world");
pause(1);
another function
pause (2);
another function
etc....
end
end
So yea, the pause is important and I don't want to touch it.
Amit
2014년 1월 28일
Keith, I understand that you dont wanna touch pause. I gave the solution accordingly. Instead of every pause, you can use getkeywait(p) which is equivalent of pause(p) but also give you another feature. Control via keyboard.
In the end, its your choice. I dont have to convince you as I am not selling anything to you. I can say 100 times that pause(p) and getkeywait(p) are similar, except that pause blindly waits for p secs while getkeywait will wait for p second but will take responses.
keith lin
2014년 1월 29일
Oh I see, I am just a bit confuse with the function. Thanks!!!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Desktop에 대해 자세히 알아보기
태그
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
