I am stuck on a problem with my MATLAB code. This should behave as a pathing program for a calculation. However, it seems to not follow the logic I instructed it. The logic is that, at the current position given, look up, right, down, and left for conditions: (1) It must not be a whitespace " ". (2) It must not go back to its past position. The program behaves well until for position (3, 6) or A(3,6) which it throws me the error code of:
Output argument "new_position" (and possibly others) not assigned a value in the execution with "test>pathfinder" function.
Error in test (line 57)
[last_direction, new_position] = pathfinder(last_direction, A, current_position);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
I've reviewed the code many times already, and even revised it for good, but the same misbehavior or error still goes. The program is expected that from the source 'S' it will path find any values that is not a whitespace(' ') and is not the direction in which it came from until it gets back to the source's position.
However, as said, at position (3,6) or A(3, 6), the logic seems to misbehave and instead of following my expected behavior, error is thrown. If it helps, the idea was that it can be any array of any design that the program can be capable of pathfinding. The current array included in the code is just for testing. The code is down below:
A = [' ', ' ', ' ', ' ', ' ', ' ', ' ';
' ', '-', '-', '2', '-', '-', ' ';
' ', 'S', ' ', ' ', ' ', '2', ' ';
' ', '-', '-', '-', '-', '-', ' ';
' ', ' ', ' ', ' ', ' ', ' ', ' '];
function source_position = source_finder(array, source_symbol)
[row, col] = find(array == source_symbol);
source_position = [row, col];
end
function [last_direction, new_position] = pathfinder(last_direction, array, current_position)
row = current_position(1);
col = current_position(2);
if (array(row - 1, col) ~= ' ')
if ~ismember("UP", last_direction)
new_position = [row - 1, col];
last_direction = "DOWN";
else
end
elseif (array(row, col + 1) ~= ' ')
if ~ismember("RIGHT", last_direction)
new_position = [row, col + 1];
last_direction = "LEFT";
else
end
elseif (array(row + 1, col) ~= ' ')
if ~ismember("DOWN", last_direction)
new_position = [row + 1, col]
last_direction = "UP"
else
end
elseif (array(row, col - 1) ~= ' ')
if ~ismember("LEFT", last_direction)
new_position = [row, col - 1];
last_direction = "RIGHT";
else
end
end
end
% main
source_position = source_finder(A, 'S');
disp(source_position)
last_direction = "NONE";
current_position = source_position;
while (true)
[last_direction, new_position] = pathfinder(last_direction, A, current_position);
current_position = new_position;
disp(current_position)
disp(last_direction)
if (current_position == source_position)
break
end
end
The overall output for the code run:
3 2
2 2
DOWN
2 3
LEFT
2 4
LEFT
2 5
LEFT
2 6
LEFT
3 6
UP
Output argument "new_position" (and possibly others) not assigned a value in the execution with "test>pathfinder" function.
Error in test (line 57)
[last_direction, new_position] = pathfinder(last_direction, A, current_position);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Where is my logic mistake?
ismemberis awkward here, because it implies a different meaning than intended. It works, but it takes a bit of effort to read. Note that if you were to use single-quote strings instead of double-quote strings, the output would be very different. Instead, usestrcmpor simply==(the latter would also do something different with single-quote strings, but you'd see error messages, not just a wrong behavior).