Using arrow keys to increment variables within a while loop
14 次查看(过去 30 天)
显示 更早的评论
I have a GUI that is constantly plotting a graph for a set period of time. I have a variable that I want to be able to change in real time with the left and right arrow keys. I've tried looking up code, but i'm having a hard time understanding what is being said.
For example, I used the code
h = figure;
set(h,'KeyPressFcn',@KeyPressCb);
function y = KeyPressCb(~,evnt)
fprintf('key pressed: %s\n',evnt.Key);
global s;
if strcmp(evnt.Key,'rightarrow')==1
s = evnt.Key;
elseif strcmp(evnt.Key, 'leftarrow')==1
s = evnt.Key;
elseif strcmp(evnt.Key,'space')==1
s = evnt.Key;
end
end
end
in an attempt to adjust the value of theta, but can't get it to work right? Any advice would be appreciated.
0 个评论
回答(2 个)
Walter Roberson
2017-5-21
if strcmp(evnt.Key, 'rightarrow')
theta = theta + 1;
elseif strcmp(evnt.Key, 'leftarrow')
theta = theta - 1;
end
Saeed Jahromi
2021-7-3
I guess you have a typo. "event" should be correct, not "evnt".
1 个评论
Walter Roberson
2021-7-4
编辑:Walter Roberson
2021-7-4
No, the user had posted
function y = KeyPressCb(~,evnt)
so the correct variable name is evnt not event .
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!