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
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
keith lin 2014 年 1 月 28 日
I don't want it to be time limited.
Amit
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
Amit 2014 年 1 月 28 日
Also, printf is not a command in Matlab.
keith lin
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".
Amit
Amit 2014 年 1 月 28 日
編集済み: Amit 2014 年 1 月 28 日
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
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
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
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
keith lin 2014 年 1 月 29 日
Oh I see, I am just a bit confuse with the function. Thanks!!!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDesktop についてさらに検索

タグ

質問済み:

2014 年 1 月 28 日

コメント済み:

2014 年 1 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by