在Octave中运行包含本地函数的简单脚本时出错 [英] Error when running simple script containing local function in Octave
问题描述
我的文件testtest.m看起来像:
My file testtest.m looks like :
pluse(1, 2)
function retval = pluse(input1, input2)
retval = input1 + input2;
endfunction
然后我得到:
error: 'pluse' undefined near line 1 column 1
error: called from
testtest at line 1 column 1
为什么会出现此错误?
Why do I get this error?
推荐答案
为正确回答您的问题,我需要指出两点:
To answer your question properly, I need to point out two things:
-
在八度音程和matlab中创建函数的规范方法是将其放在同名的专用文件中,并以 function声明启动文件.必然的结果是,在启动时会检测到路径中以函数声明开头的任何文件,并将其作为可调用函数提供给用户.
The canonical way to create a function in both octave and matlab is by placing it in a dedicated file by the same name, and starting the file with a function declaration. The corollary of this is that any file in the path that starts with a function declaration is detected at start-up and made available to the user as a callable function.
Octave多年来一直支持"on-the-go"功能定义(并且实际上还支持随后将"on-the-go"功能导出"到文件中); matlab最近才包含"on-the-go"功能(我相信是2016b),并且选择了与八度有所不同的实现方式(大概是在八度上保持八度?:p)
Octave has supported 'on-the-go' function definitions for many years (and in fact also supports subsequent 'exporting' of such 'on-the-go' functions to files); matlab has only included 'on-the-go' functionality very recently (2016b I believe?), and has chosen to implement this somewhat differently than octave, ( presumably to keep octave on its toes? :p )
Octave的实现有效地直接遵循了语言规则.任何以不是不是函数声明"的语句开头的文件都被视为脚本,即一系列独立的语句.因此,脚本开始处无害的1
就像它得到的非功能声明"语句一样简单,但实际上可以是任何东西.然后,脚本可以具有所需的多个运行中"函数定义.对于要在移动中"上下文中使用功能的语句,显然必须先定义该功能.这是从基本原理出发的:脚本与功能相比,脚本有效地表示了一组简单的语句,这些语句按顺序运行而无需任何预处理,并且可以期望将这些命令简单地复制/粘贴到命令中或从命令中复制/粘贴这些命令.终端,并希望它们运行.
Octave's implementation effectively follows straightforwardly from the rules of the language. Any file that starts with a statement that isn't a 'function declaration' is treated as a script, i.e. a sequence of independent statements. Hence the innocuous 1
at the start of your script which is as simple a 'non-function-declaration' statement as it gets, but really it could be anything. A script can then have as many 'on-the-go' function definitions as desired. For a statement to make use of a function in an 'on-the-go' context, the function clearly needs to have been defined first. This follows from the fundamental principle that a script, in contrast to a function, effectively represents a simple collection of statements that are run in sequence without any pre-processing, and that one could expect to simply copy/paste these commands to or from their terminal and expect them to run.
Matlab的最新实现有效地打破了此功能/范式.要了解我的意思,请将上面的代码复制粘贴到一个新的终端中(或突出显示然后按F9键),然后观察它是否破裂.脚本不再是可复制/可粘贴的,而是假定matlab会先读取并加载 first 的所有函数定义,然后 then 返回并尝试运行其余命令;换句话说,matlab现在有点像处理其功能一样对待脚本.请注意,matlab仍然需要脚本以非函数声明语句开头,因此有效地,此位与需要以"1"开头的八度音程相同.唯一改变的是抢先加载运行中"功能的这种先行行为,我认为这在脚本上下文中不一定是件好事.
Matlab's recent implementation effectively breaks this functionality / paradigm. To see what I mean, copy paste your code above into a fresh terminal (or highlight then press F9) and watch it break. A script is no longer copy/pasteable, but assumes matlab will read ahead and load up any function definitions first, and then go back and try to run the remaining commands; in other words, matlab now kinda treats scripts like it does its functions. Note that matlab still also requires a script to start with a non-function-declaration statement, so effectively this bit is the same as octave needing to start with a '1'. The only thing that's changed is this look-ahead behaviour for preemptively loading 'on-the-go' functions, which I would argue isn't necessarily a good thing in the context of scripts.
我认为Octave的方法更有意义,尽管您在使用Matlab时(无意地)将脚本有效地视为不是脚本时指出了便利.首先应该不应该做哪个.如果需要先行功能和嵌套,则实际上应该首先编写适当的功能并提供相关的作用域上下文.
I would argue that Octave's approach makes more sense, despite the convenience you point out with matlab when (unintentionally) treating a script as effectively not a script. Which one presumably shouldn't do in the first place. If you need look-ahead functionality and nesting, you really should be writing a proper function and providing relevant scoping context in the first place.
这篇关于在Octave中运行包含本地函数的简单脚本时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!