简体   繁体   English

八度,错误未定义变量,无法将数据数组识别为函数的输入

[英]Octave, Error undefined variable, not recognized data array as input of an function

all my variables are declared in my main script as you can see below: 我的所有变量都在主脚本中声明,如下所示:

DataArray.a = 1;
DataArray.b = 2;
d = 3;


DataArray = test1_2 (d, DataArray);

disp(DataArray);

The main script also works with some functions. 主脚本还可以使用某些功能。 A input of these functions is the data array which is declared in the main script: 这些函数的输入是在主脚本中声明的数据数组:

function DataArray = test1_2 (d, DataArray )

g = DataArray.a;
h = DataArray.b;

DataArray.result = d * g* h;
endfunction

When I run the main script the following error occurs: 当我运行主脚本时,发生以下错误:

error: 'DataArray' undefined near line 3 column 5 错误:“ DataArray”在第3行第5列附近未定义
error: called from test1_2 at line 3 column 3 错误:第3行第3列从test1_2调用

If i type in for example the DataArray.a, octave knows that it is declared and gives out "1". 例如,如果我键入DataArray.a,则八度知道它已声明并给出“ 1”。

How can I use the same data array in my main script as well as in my function? 如何在主脚本和函数中使用相同的数据数组?

When you say 当你说

DataArray = test1_2 (DataArray);

Your test1_2 function is called with one argument. 您的test1_2函数使用一个参数调用。 Inside the function: 函数内部:

function DataArray = test1_2 (d, e, f, DataArray )

only the first argument, d , is defined. 仅定义了第一个参数d The other arguments remain undefined. 其他参数仍未定义。 Note that the names of the arguments inside the function are totally unrelated to the names of variables you use to call the function. 请注意,函数内部的参数名称与您用来调用函数的变量名称完全无关。 That is, inside the function, the variable d contains whatever DataArray contains outside the function, not what is inside the variable d outside the function. 也就是说,在函数内部,变量d包含任何DataArray包含的功能之外,没有什么是变量里面d之外的功能。 Octave does not try to match up variable names like this. Octave不会尝试像这样匹配变量名。

So, given that your function needs 4 input arguments, you should call it as this: 因此,假设您的函数需要4个输入参数,则应按以下方式调用它:

DataArray = test1_2(d, e, f, DataArray )

I suggest you read the excellent tutorials from MATLAB, for example these about writing functions . 我建议您阅读MATLAB的优秀教程,例如有关编写函数的教程。 As you know, Octave mostly mimics the MATLAB syntax, so the MATLAB tutorials are also a good way to get started with Octave. 如您所知,Octave大多模仿MATLAB语法,因此MATLAB教程也是使用Octave的好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM