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 个评论
I don't want it to be time limited.
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.
Also, printf is not a command in Matlab.
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.
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.
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)
