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

 Akzeptierte Antwort

Amit
Amit am 28 Jan. 2014

0 Stimmen

I think you can use this function from fileexchange: http://www.mathworks.com/matlabcentral/fileexchange/8297-getkeywait

9 Kommentare

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Tags

Gefragt:

am 28 Jan. 2014

Kommentiert:

am 29 Jan. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by