You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MATLAB路径规划代码逻辑异常:输出变量未赋值报错排查

MATLAB路径规划程序报错分析与逻辑修正

问题描述

编写的MATLAB路径规划程序预期从源点'S'出发,在上下左右四方向寻找满足「目标位置非空格' '、不返回之前位置」的路径,直至回到源点。但运行至位置(3,6)时触发报错:

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);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

测试用数组、完整代码及运行输出如下:

测试数组与完整代码

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

程序运行输出

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);
                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

逻辑错误分析

  1. 方向判断用elseif串联,导致可行方向被忽略
    四个方向的判断采用elseif串联,只要第一个方向的位置非空格(哪怕该方向因禁止返回无法通行),后续所有方向的判断都会被跳过。例如在位置(3,6)时:

    • 上方位置(2,6)是'-'(非空格),触发第一个if块;但此时last_direction为"UP",子判断~ismember("UP", last_direction)不成立,进入空else分支无任何赋值;
    • 由于elseif的特性,后续下方(4,6)的可行方向完全不会被检查,最终导致输出参数未赋值。
  2. 无可行方向时未做处理
    当所有方向要么是空格,要么是禁止返回的方向时,函数未对new_position和last_direction进行任何赋值,触发MATLAB的输出参数未定义报错。

  3. 循环终止条件存在歧义
    current_position == source_position会返回一个逻辑向量,MATLAB中if对非标的逻辑值会要求所有元素为真才执行,但显式使用all(current_position == source_position)更清晰,避免潜在逻辑漏洞。

  4. 部分语句缺少分号
    pathfinder函数中第三个分支的赋值语句未加分号,虽非语法错误,但会导致运行时额外输出,影响输出整洁性。

修正方案

1. 修改pathfinder函数的方向判断逻辑

将elseif改为独立的if块,找到可行方向后直接返回;同时添加边界检查避免索引越界,以及无可行方向时的错误处理:

function [last_direction, new_position] = pathfinder(last_direction, array, current_position)
    
    row = current_position(1);
    col = current_position(2);
    [max_row, max_col] = size(array);
    
    % 初始化输出参数,避免未赋值
    new_position = current_position;

    % 检查上方(先判断边界)
    if row > 1 && (array(row - 1, col) ~= ' ') && ~ismember("UP", last_direction)
        new_position = [row - 1, col];
        last_direction = "DOWN";
        return;
    end
    % 检查右方
    if col < max_col && (array(row, col + 1) ~= ' ') && ~ismember("RIGHT", last_direction)
        new_position = [row, col + 1];
        last_direction = "LEFT";
        return;
    end
    % 检查下方
    if row < max_row && (array(row + 1, col) ~= ' ') && ~ismember("DOWN", last_direction)
        new_position = [row + 1, col];
        last_direction = "UP";
        return;
    end
    % 检查左方
    if col > 1 && (array(row, col - 1) ~= ' ') && ~ismember("LEFT", last_direction)
        new_position = [row, col - 1];
        last_direction = "RIGHT";
        return;
    end
    % 所有方向不可行时抛出错误
    error('当前位置[%d,%d]无可行路径', row, col);
end

2. 修正主函数的终止条件

将if (current_position == source_position)改为:

if all(current_position == source_position)
    break;
end

来源标注

内容的提问来源于stack exchange,提问作者William John Otsuro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 04:05:54