Octave says variables are undefined even when they are

末鹿安然 提交于 2019-12-25 12:51:43

问题


I have the following code

function W = robust(a,c,y)
  W = pinv(a' * c * a) * a' * c *y;
endfunction

However, when I try running it, I get the following error.

error: 'a' undefined near line 4 column 12
error: called from
    robust at line 4 column 5

This makes no sense at all. What am I doing wrong here?

EDIT: I'm calling this via emacs. So, I'm using the command 'octave-source-file', which executes the file in the inferior octave process. I see that it works if this buffer isn't where I start the process. If I start the octave process on a different file and then send this file to the inferior process, it evaluates the function.


回答1:


Octave has inherited from MATLAB a concept of "function files", different from "script files". A "function file" is assumed to contain a function that needs to be called as the file is executed. So when you think "I'm defining it", Octave thinks you are calling that function. And since you gave no inputs, there is that "undefined variable" error.

Function files:

Once Octave finds a file with a name that matches, the contents of the file are read. If it defines a single function, it is compiled and executed.

Script files:

Unlike a function file, a script file must not begin with the keyword function. If it does, Octave will assume that it is a function file, and that it defines a single function that should be evaluated as soon as it is defined.



来源:https://stackoverflow.com/questions/47488479/octave-says-variables-are-undefined-even-when-they-are

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!