Press a key to stop the program?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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 日
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
Amit
2014 年 1 月 28 日
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
keith lin
2014 年 1 月 28 日
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 についてさらに検索
タグ
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
