查看单个帖子
旧 2019-12-05, 23:04   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,008
声望力: 66
poster 正向着好的方向发展
默认 重写但未重新定义已定义函数的“未定义”错误

这有效(画出“空”图):

function testme plot(1) 这有效(返回1):

function testme plot = @(x)x; plot(1) 这不是(错误:“未定义的函数或变量'图'。”):

function testme if 0 plot = @(x)x; end plot(1) 这里发生了什么?为什么重写但未重新定义已定义的函数会导致该函数未定义?

注1:这不是内置函数专用的;以下代码返回相同的错误:

function testme if 0 myfun = @(x)x; end myfun(1) function x=myfun(x) x=x*2; 注2:错误发生在函数环境中,而不是脚本中;以下代码不返回错误(并且绘制与第一个示例相同的空图):

if 0 plot = @(x)x; end plot(1)

更多&回答...
poster 当前离线   回复时引用此帖