Using arrow keys to increment variables within a while loop

조회 수: 15 (최근 30일)
Zachary Rich
Zachary Rich 2017년 5월 21일
편집: Walter Roberson 2021년 7월 4일
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.

답변 (2개)

Walter Roberson
Walter Roberson 2017년 5월 21일
if strcmp(evnt.Key, 'rightarrow')
theta = theta + 1;
elseif strcmp(evnt.Key, 'leftarrow')
theta = theta - 1;
end
  댓글 수: 1
Zachary Rich
Zachary Rich 2017년 5월 21일
I'm getting an error,
Undefined variable "evnt" or class "evnt.Key".

댓글을 달려면 로그인하십시오.


Saeed Jahromi
Saeed Jahromi 2021년 7월 3일
I guess you have a typo. "event" should be correct, not "evnt".
  댓글 수: 1
Walter Roberson
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 CenterFile 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!

Translated by