How can you move a plot using a keyboard input?
Mostrar comentarios más antiguos
I have asked a similar question before, but I think it would be easier to find an answer since I changed my code a bit. I have a plot that contains a circle. I want to change move the center of the circle using keyboard input.
function h = circle(xCenter,yCenter,radius)
hold on
th = 0:pi/50:2*pi;
xunit = radius * cos(th) + xCenter;
yunit = radius * sin(th) + yCenter;
h = plot(xunit, yunit);
hold off
end
xCenter = 330;
yCenter = 255;
radius = 16;
%ship = viscircles([xCenter, yCenter], radius, 'Color', 'c');
%My previous code used viscircle but I realized that plot works
%just fine with my code. also i think it is easier to get
%numerical data using plot
S.ship = circle(xCenter, yCenter, radius);
set(gcf(), 'KeyPressFcn', @fig_kpfcn);
%can you set a plot as a figure? I have a background image
%right now that the figure initially was set to. this part of
%the code occurs when the a button is pressed and the figure
%changes background and gets rid of all existing buttons.
%However, it is still the same figure. i'm using gcf() cuz i
%don't know what else to use.
hold off
guidata(S.ship,S);
%this is where i'm not sure if i coded everything correctly. I followed a previous mathworks answer page
%https://www.mathworks.com/matlabcentral/answers/8790-reading-arrow-key-input?s_tid=srchtitle
%but i don't know if i understood it completely. Same for the
%code below. i did almost directly copy the code, but i just
%wanted to make sure it works before changing any numbers.
function [] = fig_kpfcn(varargin)
% Figure (and pushbutton) keypressfcn
S = guidata(H);
P = get(S.ship,'position');
%set(s.h, 'KeyPressFcn', E.Key)
switch E.Key
case 'rightarrow'
set(S.ship,'pos',P+[5 0 0 0])
case 'leftarrow'
set(S.ship,'pos',P+[-5 0 0 0])
case 'uparrow'
set(S.ship,'pos',P+[0 5 0 0])
case 'downarrow'
set(S.ship,'pos',P+[0 -5 0 0])
otherwise
end
Any help would be appreciated.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Board games en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!