MATLAB Answers

0

Using numpad 8 2 6 4 as North South East West

Asked by Samuel Angelo Regalado on 11 Apr 2019
Latest activity Answered by Jos (10584)
on 11 Apr 2019
Hi! I wanted to create a code where I can plot a variable using my numpad 8, 2, 6, 4, as North, South, East, West. The distance is also coded as an integer value. For example, the inputs 8 5 will mean the variable will move towards the North by a distance of 5 units. Assuming that the variable is initially at location (0,0)

  1 Comment

I have created a code but my code only seems to plot (x,y) variable :(( sorry I just started coding excuse my mistakes
choice = menu('Select one', '(1) Start', ' (2) Reset', '(3) Quit', '(0) Location');
if choice==1
clear;
clear all;
clc;
m=10;
for n = 1:m-1;
if n == 1;
xn = input('Input Direction: ');
fprintf('\n');
yn = input('Input Distance: ');
fprintf('\n');
x = [xn];
y = [yn];
fprintf('%d \n\n %f', x, y);
fprintf('\n\n');
plot(x,y,'-o','MarkerEdgeColor','red');
else
xn = input('Input Direction: ');
fprintf('\n');
yn = input('Input Distance: ');
fprintf('\n');
xadd = x(n-1);
yadd = y(n-1);
x = [x xn+xadd];
y = [y yn+yadd];
fprintf('\n\n');
xor = 0;
line([-max(x),max(x)],[xor,xor]);
hold on
plot([0 0],[-max(y) max(y)]);
hold on
plot(x,y,'-o','MarkerEdgeColor','red');
grid on;
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
end
end
fprintf('here are your values of x and y\n')
x
fprintf('\n');
y
elseif choice==2
clear;
clear all;
clc;
m=5;
for n = 1:m-1;
if n == 1;
xn = input('Input Direction: ');
fprintf('\n');
yn = input('Input Distance: ');
fprintf('\n');
x = [xn];
y = [yn];
fprintf('%d \n\n %f', x, y);
fprintf('\n\n');
plot(x,y,'-o','MarkerEdgeColor','red');
else
xn = input('Input Direction: ');
fprintf('\n');
yn = input('Input Distance: ');
fprintf('\n');
xadd = x(n-1);
yadd = y(n-1);
x = [x xn+xadd];
y = [y yn+yadd];
fprintf('\n\n');
xor = 0;
line([-max(x),max(x)],[xor,xor]);
hold on
plot([0 0],[-max(y) max(y)]);
hold on
plot(x,y,'-o','MarkerEdgeColor','red');
grid on;
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
end
end
fprintf('here are your values of x and y\n')
x
fprintf('\n');
y
elseif choice==3
disp = ('Normal program termination. Goodbye!')
else
plot(choice==1,'-o','MarkerEdgeColor','red');
end

Sign in to comment.

Tags

1 Answer

Answer by Jos (10584)
on 11 Apr 2019

Perphaps my getkey function may be of use to you:

  0 Comments

Sign in to comment.