Using arrow keys to increment variables within a while loop

19 visualizaciones (últimos 30 días)
Zachary Rich
Zachary Rich el 21 de Mayo de 2017
Editada: Walter Roberson el 4 de Jul. de 2021
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.

Respuestas (2)

Walter Roberson
Walter Roberson el 21 de Mayo de 2017
if strcmp(evnt.Key, 'rightarrow')
theta = theta + 1;
elseif strcmp(evnt.Key, 'leftarrow')
theta = theta - 1;
end

Saeed Jahromi
Saeed Jahromi el 3 de Jul. de 2021
I guess you have a typo. "event" should be correct, not "evnt".
  1 comentario
Walter Roberson
Walter Roberson el 4 de Jul. de 2021
Editada: Walter Roberson el 4 de Jul. de 2021
No, the user had posted
function y = KeyPressCb(~,evnt)
so the correct variable name is evnt not event .

Iniciar sesión para comentar.

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by