How can you move a plot using a keyboard input?

조회 수: 9 (최근 30일)
NinaCodes
NinaCodes 2022년 4월 15일
댓글: Voss 2022년 4월 15일
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.

채택된 답변

Voss
Voss 2022년 4월 15일
편집: Voss 2022년 4월 15일
I think you're right that plot is probably easier to work with than viscircles, and your circle function appears to work as-is.
The main thing to fix in your code is the definition of the function fig_kpfcn, which had been adapted from the other answer. That answer was about moving a figure by setting its 'Position' property, and the analogous thing for a line object (such as the plotted circle here) would be to set its 'XData' and 'YData' properties. See my adapted fig_kpfcn below.
In particular, note that fig_kpfn has two input arguments: H - the figure, and E - the so-called "event data" (both of which appeared in the other answer but disappeared in your code).
Without seeing exactly how the code you've shared fits in with the rest of the GUI, I can't say for sure you could copy and paste this code into yours and have it work without modification, but maybe seeing fig_kpfcn here is sufficient for you to see what you need to do in your code to get it to work.
xCenter = 330;
yCenter = 255;
radius = 16;
S.ship = circle(xCenter, yCenter, radius);
set(gcf(), 'KeyPressFcn', @fig_kpfcn);
guidata(S.ship,S);
function [] = fig_kpfcn(H,E)
% Figure (and pushbutton) keypressfcn
S = guidata(H);
switch E.Key
case 'rightarrow'
set(S.ship,'XData',get(S.ship,'XData')+5);
case 'leftarrow'
set(S.ship,'XData',get(S.ship,'XData')-5);
case 'uparrow'
set(S.ship,'YData',get(S.ship,'YData')+5);
case 'downarrow'
set(S.ship,'YData',get(S.ship,'YData')-5);
end
end
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
  댓글 수: 6
NinaCodes
NinaCodes 2022년 4월 15일
Thank you so much!! I've been trying to figure out why it wasn't moving for days now! I tweaked the code abit to (mostly position numbers), but my code is working now and circle moves! :D
Voss
Voss 2022년 4월 15일
Excellent!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Board games에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by