【问题标题】:Error message: “Output argument (and maybe others) not assigned during function call”错误消息:“函数调用期间未分配输出参数(可能还有其他参数)”
【发布时间】:2017-02-03 13:08:41
【问题描述】:

我有这个函数,它接受图像的行/列坐标并返回相邻像素变化的方向。

function [d] = p_directions(row, col, img)

if img(row, col+1) == 2
  if img(row, col) == 3
    d = 'A+'; 
  elseif img(row, col) == 1
    d = 'B+';    
  elseif img(row, col) == 2
    d = NaN;
  end
end

if img(row, col) == 2
   if img(row, col+1) == 3
    d = 'A-';
   elseif img(row, col+1) == 1
    d = 'B-';
   end
end
end

函数调用:

[row, col] = find_row_col(A); [d] = p_directions(row, col, img)

错误信息:

Error in p_directions (line 15) if img(row, col + 1) == 2

Output argument "d" (and maybe others) not assigned during call to "p_directions".

我想相信错误来自我脚本的第一行(“第 15 行”),在这种情况下,甚至不会计算变量“d”。我是编程新手,我不知道函数脚本的第一行到底有什么问题?请对此有任何帮助,建议或意见?提前谢谢你。

【问题讨论】:

    标签: matlab image-processing


    【解决方案1】:

    问题在于,如果您的任何嵌套 if 条件为真,您的函数 p_directions 只会为 d 赋值。如果它们都不为真,则您尚未分配要返回的“默认”值。

    您的两个条件块分别以if img(row, col+1) == 2if img(row, col) == 2 开头。如果它们都不是真的,因为img(row,col)img(row,col+1) 都不是 2,怎么办?然后d 将没有值,Matlab 不知道返回什么。因此出现错误。

    【讨论】:

      【解决方案2】:

      这是错误说明一切的一种情况......

      您需要为d 指定一个默认值。通常你会选择一个值,这样当它被返回时你就知道出了问题。所以直接在函数调用之后,您可能会考虑放置类似

      d = -1
      

      或者你可以添加 else 语句...

      if  
         ...
      else 
         d = -1
      

      问题是,如果img 值在您指定的任一点处为2,您将永远进入您的if 语句。如果没有,则永远不会分配返回值。

      【讨论】:

      • 非常感谢大家。我现在在朋友的帮助下解决了这个问题。问题实际上在于调用函数的循环。至于你的 cmets,你是绝对正确的,因为即使循环是正确的,情况也是如此。我想我刚刚从这个错误中学到了很多东西。
      猜你喜欢
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多