在函数中运行脚本时出现令人费解的错误 [英] Puzzling error with script run in function

查看:126
本文介绍了在函数中运行脚本时出现令人费解的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab R2012b中遇到一个令人费解的错误.似乎变量名也是数据类型,表现出奇怪的行为.请看这个小例子:

I'm experiencing a puzzling error in Matlab R2012b. It seems that variable names that are also data types exhibit strange behavior. Please see this small example:

function [] = test1()
dataset = 1;

if dataset ~= 0
  disp hello
end

end

test1()的调用将产生预期的输出hello. 现在,我运行脚本而不是在函数中设置dataset的值.

A call to test1() produces output hello, as expected. Now, rather than set the value of dataset in my function, I run a script instead.

function [] = test2()
myscript;

if dataset ~= 0
  disp hello
end

end

其中myscript.m有一行:

dataset=1;

现在,当我致电test2()时,出现此错误:

Now, when I call test2() I get this error:

Undefined function 'ne' for input arguments of type 'dataset'.
Error in test2 (line 4)
if dataset ~= 0 

(请原谅名为dataset的变量-我知道它也是数据类型的名称,并且它出现在我运行的代码中.)因此,似乎在test2中,Matlab创建了一个空dataset对象,而不是使用名为dataset的变量.此外,仅当我在脚本而不是函数主体中设置值时,才会出现此行为.更奇怪的是,我可以做到:

(Forgive the variable named dataset - I know that it is also the name of a data type, and it came in the code I was running.) So it seems as if in test2, Matlab creates an empty dataset object rather than using the variable named dataset. Furthermore, this behavior only appears when I set the value in a script rather than in the function body. Even more weird, is that I can do:

>> dbstop in test2 at 4 % line of if statement
>> test2()
K>> dataset
dataset =
      1.00
K>> dataset ~= 0
ans =
 1
K>> if dataset ~= 0, disp hello; end
hello
K>> dbcont

,我得到同样的错误!该错误不会在调试模式下显示,但可以正常执行.

and I get the same error! The error is not displayed in debugging mode but it is in normal execution.

任何人都可以复制吗?这是怎么回事?

Can anyone reproduce this? What is going on here?

推荐答案

MATLAB在线帮助中有一些页面介绍了该问题. 变量名称

The MATLAB online help has some pages dealing with this issue; Variables Names and Loading Variables within a Function seem to be the most relevant.

没有明确的页面讨论MATLAB在编译时如何解析名称,但是

There is no explicit page that discusses how MATLAB resolves names at compilation time, but there is one little tidbit at the bottom of the Variables Names page: "In some cases, load or eval add variables that have the same names as functions. Unless these variables are in the function workspace before the call to load or eval, the MATLAB parser interprets the variable names as function names."

换句话说,如果解析器找到名称与另一个现有对象相同的变量的显式赋值,则本地定义优先. 在您的test2()中,没有对变量dataset的显式分配;因此,在编译文件时,解析器将dataset解释为类构造函数(因为解析器将不会运行或将myscript内联到函数中).

In other words, if the parser finds an explicit assignment to a variable whose name is the same as another existent object, the local definition takes precedence. In your test2(), there is no explicit assignment to a variable dataset; therefore, when the file is compiled, the parser interprets dataset to be a class constructor (since the parser will not run or inline myscript into the function).

然后在运行时,即使名为dataset的变量已被伪装 1 到函数的工作区中,正在运行的解释代码在dataset符号>-与类构造函数关联的语句.

Then at run-time, even though a variable named dataset has been poofed1 into the function's workspace, the interpreted code that is running still has the dataset symbol in the if-statement associated with the class constructor.

如果需要,您仍然可以使用dataset变量名并从外部文件加载,但是应该通过函数调用进行显式赋值来完成.例如:

If you need to, you can still use the dataset variable name and load from an external file, but it should be done with an explicit assignment via a function call. For example:

dataset = initialize();

现在解析器将注意到dataset是函数initialize的任意输出,并且一切都会好起来.实际上,如果需要,甚至可以让initializedataset构造函数返回到dataset变量.

Now the parser will notice that dataset is some arbitrary output of the function initialize and all will be well. In fact, you can have even have initialize return a dataset constructor to the dataset variable if you wanted.


1 如果在没有显式分配的情况下定义变量,MATLAB人员(至少在我读过的某些博客中)将其称为欺骗".使用不带任何输出参数的load,使用eval以及仅运行脚本(而非函数)都可以将变量插入工作空间.只要变量名在编译时不与其他使用中的符号冲突,此方法就可以正常工作.


1 When variables are defined without explicit assignment, MATLAB people (at least on some of their blogs I've read) called this 'poofing'. Using load without any output arguments, using eval, and simply running scripts (not functions) can all poof variables into the workspace. This can work fine as long as the variable names do not conflict with other in-use symbols at compile time.

这篇关于在函数中运行脚本时出现令人费解的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆