八度表示变量即使在未定义的情况下也未定义 [英] Octave says variables are undefined even when they are

查看:146
本文介绍了八度表示变量即使在未定义的情况下也未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

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?

我通过emacs称呼它.因此,我正在使用命令"octave-source-file",该命令在次等的八度音程中执行文件.我看到,如果此缓冲区不在我启动进程的地方,它将起作用.如果我在另一个文件上启动八度音程,然后将此文件发送到下一个音程,它将评估该功能.

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.

推荐答案

Octave从MATLAB继承了功能文件"的概念,与脚本文件"不同.假定功能文件"包含在执行文件时需要调用的功能.因此,当您认为我正在定义它"时,Octave会认为您正在调用该函数.而且由于您未提供任何输入,因此存在未定义的变量"错误.

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.

功能文件:

一旦Octave找到一个名称匹配的文件,就会读取该文件的内容.如果定义了单个函数,则将编译并执行该函数.

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.

脚本文件:

与功能文件不同,脚本文件不能以关键字function开头.如果是这样,Octave将假定它是一个功能文件,并且它定义了一个应在定义后立即求值的功能.

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.

这篇关于八度表示变量即使在未定义的情况下也未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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