rectangle command not working in a function with multiple variables in the coordinate argument

1 Ansicht (letzte 30 Tage)
This has stumped me for a while now and it's very annoying.
this is my function, it uses the KeyPressFcn to move a rectangle up, down, left and right with the arrow keys:
figure('Color',[0.1 .6 0],'Name',...
'Highwayman','MenuBar','none','NumberTitle','off','Resize','off',...
'position',[500,100,400,800],'KeyPressFcn',@userkey);
axis([0,5,0,20])
set(gca,'color','black')
driverx = 2;
drivery = 1;
startingcar = rectangle('position',[driverx+.25 drivery .5 2],'EdgeColor','r','Curvature',[0.5,1],'FaceColor','red');
function userkey(src,event)
global driverx drivery redcar startingcar
k = event.Key;
%disp(event.Key)
switch k
case 'leftarrow'
driverx = driverx-1;
delete(redcar)
if driverx<0
driverx = 0;
end
redcar = rectangle('position',[driverx+.25 drivery .5 2],'EdgeColor','r','Curvature',[0.5,1],'FaceColor','red');
case 'rightarrow'
driverx = driverx+1;
delete(redcar)
if driverx>4
driverx = 4;
end
%rightturn = [driverx+.25 drivery .5 2];
redcar = rectangle('position',[driverx+.25 drivery .5 2],'EdgeColor','r','Curvature',[0.5,1],'FaceColor','red');
case 'uparrow'
drivery = drivery+2;
delete(redcar)
if driverx>18
driverx = 18;
end
redcar = rectangle('position',[driverx+.25 drivery .5 2],'EdgeColor','r','Curvature',[0.5,1],'FaceColor','red');
case 'downarrow'
drivery = drivery-2;
delete(redcar)
if driverx<1
driverx = 1;
end
redcar = rectangle('position',[driverx+.25 drivery .5 2],'EdgeColor','r','Curvature',[0.5,1],'FaceColor','red');
end
end
Whenever the user presses a key it should move the car in that direction, with if statements preventing it from leaving the border, and indeed when it is just left and right, where drivery is replaced with just the number 1 in the rectangle command, it works fine, but when I add the second dimension of up and down, it gives an error when I try to move in any direction:
Error using rectangle
Value must be a 4 element vector
Error in Highwayman>userkey (line 143)
redcar = rectangle('position',[driverx+.25 drivery .5 2],'EdgeColor','r','Curvature',[0.5,1],'FaceColor','red');
Error using Highwayman (line 103)
Error while evaluating Figure KeyPressFcn.
It's referring to [driverx+.25 drivery .5 2] because it works fine when it's just left and right: [driverx+.25 1 .5 2]. The value is definitely a 4 element vector, it looking like [2.25 1 0.5 2] if assigned to a variable and the variable put in its place but it returns the same error and doesn't move the rectangle for any input.
The rectangle function with two variables works fine outside that function and in the command window, it's just there where it doesn't work. Any help?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 5 Dez. 2021
driverx = 2;
drivery = 1;
You do not declare those as global in the function you make the assignments in.
global driverx drivery redcar startingcar
Because you did not initialize those after they were declared global, those will be empty.
case 'rightarrow'
driverx = driverx+1;
delete(redcar)
if driverx>4
driverx = 4;
end
In that code, you adjust the value of driverx by 1. Then you test whether driverx exceeds a particular value and if so change it.
case 'uparrow'
drivery = drivery+2;
delete(redcar)
if driverx>18
driverx = 18;
end
In that code, you adjust the value of drivery by 2. Then you test whether driverx exceeds a certain value, and if so change it.
Why are you asymmetric in the cases? Left and right, you adjust driverx and then test driverx, but for up and down, you adjust drivery and then test driverx ??
  1 Kommentar
Nathaniel Stringer
Nathaniel Stringer am 5 Dez. 2021
Thank you, the driverx on uparrow and downarrow was an oversight by me, and making those variables global in the original function worked, despite not being needed when it's just one variable or the other being used.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by