4

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?

1
  • 1
    The use of ismember is 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, use strcmp or simply == (the latter would also do something different with single-quote strings, but you'd see error messages, not just a wrong behavior). Commented Oct 22 at 5:47

2 Answers 2

4

The logic flaw is the following:

if (array(row - 1, col) ~= ' ')
        if ~ismember("UP", last_direction)
            new_position = [row - 1, col];
            last_direction = "DOWN";
        %else   % Why have an empty else???
        end
elseif (array(row, col + 1) ~= ' ')

if (array(row - 1, col) ~= ' ')==TRUE BUT ~ismember("UP", last_direction)==FALSE then this code does never reach the elseif and terminates instead. It gets to the first if, just does nothing inside.

I think you just want to add a return after each last_direction and replace your elseif by if .

Consider using conditional breakpoints to debug your code: MATLAB debugging: smarter way to stop the code with an specific condition?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, turns out you're right, it was because of the nesting misusage. I implemented a better conditional, and it now behaves as to what I expected.
2

As said in the other answer, the flaw was actually because of nesting misusage, turns out that the fault was because of the bad conditional statement use. I implemented a better conditional and now behaves as exactly as I require it. The code is below:

function [last_direction, new_position] = pathfinder(last_direction, array, current_position)
    
    row = current_position(1);
    col = current_position(2);

    if (array(row - 1, col) ~= ' ' && ~ismember("UP", last_direction))
        new_position = [row - 1, col];
        last_direction = "DOWN";
    
    elseif (array(row, col + 1) ~= ' ' && ~ismember("RIGHT", last_direction))
        new_position = [row, col + 1];
        last_direction = "LEFT";

    elseif (array(row + 1, col) ~= ' ' && ~ismember("DOWN", last_direction))
        new_position = [row + 1, col];
        last_direction = "UP";

    elseif (array(row, col - 1) ~= ' ' && ~ismember("LEFT", last_direction))
        new_position = [row, col - 1];
        last_direction = "RIGHT";
    
    else
        error("Possibly an open path!")

    end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.