# Math Help - Matlab while function

1. ## Matlab while function

i have done the following code for a robot moving through a matrix.

Just want to know if i want to plot the outputs from the while function. Is there any way to save the outputs as a vector array so its able to be ploted or does anyone have any ideas for plotting hte points.

%clear the screen
clc
%This will read CSV file wharf.csv and name it strad
%set up a loop for which when a value inputted is invalid, an new input
a = 1;
b = 1;
c = 1;

%determine the size of the matrix

while a==1

%input in start value row and column
s_r = input('Input Starting Row ');
s_c = input(
'Input Starting Column ');

%Test to see if starting point is inside the range of the data
while s_r < 0 || s_r > n_row || s_c < 0 || s_c > n_col
disp(
'This point is outside the bounds')
s_r = input(
'Input Starting Row ');
s_c = input(
'Input Starting Column ');

end

%test to see if the coordinates are occupied by another container
disp(
'This space is already occupied by another container')
a=1;
else a=0;

while b==1
%input in goal value row and column
g_r = input('Input Goal Row ');
g_c = input(
'Input Goal Column ');

%Test to see if Goal point is inside the range of the data
while g_r < 0 || g_r > n_row || g_c < 0 || g_c > n_col
disp(
'This point is outside the bounds')
g_r = input(
'Input Goal Row ');
g_c = input(
'Input Goal Column ');

end

%test to see if the coordinates are occupied by another container
disp(
'This space is already occupied by another container')
b=1;

else disp('These are valid cordinates')
b=0;

end
end
end
end
disp(['Start matrix coordinates (', num2str(s_r,3), ', ' num2str(s_c,3) ')'])
disp([
'Goal matrix coordinates (', num2str(g_r,3), ', ' num2str(g_c,3) ')'])

%maximum number of steps allowed (to stop the robot wandering aimlessly)
M = input('Input maximum steps ');

%To start with the steps are zero
steps = 0;

%Prints the Heading for the table
fprintf('\t\tRow \t Column \t\n')

%while function to loop the random moves, the while function will only
%break if goal is reached or steps == M
while c == 1
R = 0;
C = 0;

%This if statement will decide randomly if the row or column will be
%changed as the robot cannot move diagonally

D = ceil(rand*3) - 2;
if D <= 0
R = ceil(rand*3) - 2;
else
C = ceil(rand*3) - 2;
end
%This will randomly give a value of 1, -1, or 0

%This makes sure that if the value chosen is outside the
%bounds, or has a value of 1 in the matrix or if both
%random moves give a zero value then new numbers will be
%calculated at random.
while (s_r+R)<0 || (s_r+R)>n_row || (s_c+C)<0 || (s_c+C)>n_col || s_r==-R || s_c==-C || (C==0 && R==0) || strad((s_r+R),(s_c+C))==1

D = ceil(rand*3) - 2;
if D <= 0
R = ceil(rand*3) - 2;
else
C = ceil(rand*3) - 2;
end
end
%This will add R and C onto s_r and s_c, effectively moving the robot.
s_r = (R+s_r);
s_c = (C+s_c);

%prints the values of the rows and columns so you can view how the robot is
%moving
fprintf( '%10.0f\t', s_r)
fprintf(
'%10.0f\t\n', s_c)

%calculates the number of steps as the while function is completed
%each time it doesn't reach its goal
steps = steps +1;

%This if function is used to break the while function if
%the max steps is reached or if the goal co-ordinates are
%obtained
if s_r == g_r && s_c == g_c || steps == M;
c = 0;
else c = 1;
end
end

%Display final sentence about whether or not the robot reached its goal and in how many steps
if steps == M
disp(
'Maximum steps reached')
else
disp([
'Valid path found in ', num2str(steps,5), ' steps'])
end

cheers

2. Originally Posted by smallpat
i have done the following code for a robot moving through a matrix.

Just want to know if i want to plot the outputs from the while function. Is there any way to save the outputs as a vector array so its able to be ploted or does anyone have any ideas for plotting hte points.

%clear the screen
clc
%This will read CSV file wharf.csv and name it strad
%set up a loop for which when a value inputted is invalid, an new input
a = 1;
b = 1;
c = 1;

%determine the size of the matrix

while a==1

%input in start value row and column
s_r = input('Input Starting Row ');
s_c = input(
'Input Starting Column ');

%Test to see if starting point is inside the range of the data
while s_r < 0 || s_r > n_row || s_c < 0 || s_c > n_col
disp(
'This point is outside the bounds')
s_r = input(
'Input Starting Row ');
s_c = input(
'Input Starting Column ');

end

%test to see if the coordinates are occupied by another container
disp(
'This space is already occupied by another container')
a=1;
else a=0;

while b==1
%input in goal value row and column
g_r = input('Input Goal Row ');
g_c = input(
'Input Goal Column ');

%Test to see if Goal point is inside the range of the data
while g_r < 0 || g_r > n_row || g_c < 0 || g_c > n_col
disp(
'This point is outside the bounds')
g_r = input(
'Input Goal Row ');
g_c = input(
'Input Goal Column ');

end

%test to see if the coordinates are occupied by another container
disp(
'This space is already occupied by another container')
b=1;

else disp('These are valid cordinates')
b=0;

end
end
end
end
disp(['Start matrix coordinates (', num2str(s_r,3), ', ' num2str(s_c,3) ')'])
disp([
'Goal matrix coordinates (', num2str(g_r,3), ', ' num2str(g_c,3) ')'])

%maximum number of steps allowed (to stop the robot wandering aimlessly)
M = input('Input maximum steps ');

%To start with the steps are zero
steps = 0;

%Prints the Heading for the table
fprintf('\t\tRow \t Column \t\n')

%while function to loop the random moves, the while function will only
%break if goal is reached or steps == M
while c == 1
R = 0;
C = 0;

%This if statement will decide randomly if the row or column will be
%changed as the robot cannot move diagonally

D = ceil(rand*3) - 2;
if D <= 0
R = ceil(rand*3) - 2;
else
C = ceil(rand*3) - 2;
end
%This will randomly give a value of 1, -1, or 0

%This makes sure that if the value chosen is outside the
%bounds, or has a value of 1 in the matrix or if both
%random moves give a zero value then new numbers will be
%calculated at random.
while (s_r+R)<0 || (s_r+R)>n_row || (s_c+C)<0 || (s_c+C)>n_col || s_r==-R || s_c==-C || (C==0 && R==0) || strad((s_r+R),(s_c+C))==1

D = ceil(rand*3) - 2;
if D <= 0
R = ceil(rand*3) - 2;
else
C = ceil(rand*3) - 2;
end
end
%This will add R and C onto s_r and s_c, effectively moving the robot.
s_r = (R+s_r);
s_c = (C+s_c);

%prints the values of the rows and columns so you can view how the robot is
%moving
fprintf( '%10.0f\t', s_r)
fprintf(
'%10.0f\t\n', s_c)

%calculates the number of steps as the while function is completed
%each time it doesn't reach its goal
steps = steps +1;

%This if function is used to break the while function if
%the max steps is reached or if the goal co-ordinates are
%obtained
if s_r == g_r && s_c == g_c || steps == M;
c = 0;
else c = 1;
end
end

%Display final sentence about whether or not the robot reached its goal and in how many steps
if steps == M
disp(
'Maximum steps reached')
else
disp([
'Valid path found in ', num2str(steps,5), ' steps'])
end

cheers
Code:
>help hold
or create a buffer and store the data you want to plot into the buffer for plotting later.

CB