Matlab - 在函数内使用未定义的函数/值

时间:2022-09-21 22:49:58

Lets start with an example. Define a 1-D vector in the base Workspace:

让我们从一个例子开始。在基础工作区中定义一维向量:

q = [1 0 2 4 2 3 4 2 1 0 2]; % Defined 1-D vector, already in Workspace.

This is in an M-file:

这是一个M文件:

function vector = r(i)
vect(i) = q(3*i-2:3*i-1);

If the function is called, for example,

如果调用该函数,例如,

r(2);

Matlab throws an error: "Undefined function or method 'q' for input arguments of type 'double'".

Matlab抛出一个错误:“未定义的函数或方法'q'用于'double'类型的输入参数”。

My question is: is there a way to make Matlab take the vector q "for granted" when calling the function rq is already defined so technically it could collected from the base Workspace to use it.

我的问题是:有没有办法让matlab在调用函数时将“q”视为“理所当然”r-q已经定义,所以从技术上讲它可以从基础工作空间中收集来使用它。

Of course the obvious solution is to add q to the argument list:

当然,显而易见的解决方案是在参数列表中添加q:

function vector = r(i,q)
...

and then call it with

然后用它来调用它

r(2,q)

Eventually I will, but I just want to know if there is something I don't know since this could be a pretty useful feature.

最终我会,但我只是想知道是否有一些我不知道的东西,因为这可能是一个非常有用的功能。

2 个解决方案

#1


You can use global variables, but you shouldn't. They're inefficient too.

您可以使用全局变量,但不应该。它们效率也很低。

If you for some reason don't just want to pass q in as a second argument, here are two better alternatives.

如果你出于某种原因不仅仅想将q in作为第二个参数传递,那么这里有两个更好的选择。


1. Closure via anonymous function

1.通过匿名函数关闭

If your function is simple and can be written on one line you can create an anonymous function after defining your q vector:

如果你的函数很简单并且可以写在一行上,你可以在定义q向量后创建一个匿名函数:

q = [1 0 2 4 2 3 4 2 1 0 2];
r = @(i)q(3*i-2:3*i-1);

In this case, the function r is a closure in that it captures the predefined q (note that r in this case is slightly different from that in your question). You can call this now with:

在这种情况下,函数r是一个闭包,因为它捕获预定义的q(请注意,在这种情况下,r与您的问题中的r略有不同)。你现在可以用以下方式调用:

out = r(2)


2. Shared variables via sub-function

2.通过子功能共享变量

A more general option is to use a sub-function inside your main M-file function:

更通用的选项是在主M-file函数中使用子函数:

function mainFun
    q = [1 0 2 4 2 3 4 2 1 0 2];
    r(i);

    function vect = r(i)
        vect(i) = q(3*i-2:3*i-1);
    end
end

The vector q is shared between the outer mainFun and the sub-function r. The Matlab Editor should make this clear to you by coloring q differently. More details and examples here.

向量q在外部mainFun和子函数r之间共享。 Matlab编辑器应该通过对q进行不同的着色来清楚地告诉你。这里有更多细节和例子。

#2


You can use global variables MATLAB:

您可以使用全局变量MATLAB:

1) in the workspace, declare q as global, than set its value

1)在工作空间中,将q声明为全局,而不是设置其值

global q;
q = [1 0 2 4 2 3 4 2 1 0 2];

2) in your function, declare q as global and use it:

2)在你的函数中,将q声明为全局并使用它:

function vector = r(i)
global q;
vect(i) = q(3*i-2:3*i-1);

#1


You can use global variables, but you shouldn't. They're inefficient too.

您可以使用全局变量,但不应该。它们效率也很低。

If you for some reason don't just want to pass q in as a second argument, here are two better alternatives.

如果你出于某种原因不仅仅想将q in作为第二个参数传递,那么这里有两个更好的选择。


1. Closure via anonymous function

1.通过匿名函数关闭

If your function is simple and can be written on one line you can create an anonymous function after defining your q vector:

如果你的函数很简单并且可以写在一行上,你可以在定义q向量后创建一个匿名函数:

q = [1 0 2 4 2 3 4 2 1 0 2];
r = @(i)q(3*i-2:3*i-1);

In this case, the function r is a closure in that it captures the predefined q (note that r in this case is slightly different from that in your question). You can call this now with:

在这种情况下,函数r是一个闭包,因为它捕获预定义的q(请注意,在这种情况下,r与您的问题中的r略有不同)。你现在可以用以下方式调用:

out = r(2)


2. Shared variables via sub-function

2.通过子功能共享变量

A more general option is to use a sub-function inside your main M-file function:

更通用的选项是在主M-file函数中使用子函数:

function mainFun
    q = [1 0 2 4 2 3 4 2 1 0 2];
    r(i);

    function vect = r(i)
        vect(i) = q(3*i-2:3*i-1);
    end
end

The vector q is shared between the outer mainFun and the sub-function r. The Matlab Editor should make this clear to you by coloring q differently. More details and examples here.

向量q在外部mainFun和子函数r之间共享。 Matlab编辑器应该通过对q进行不同的着色来清楚地告诉你。这里有更多细节和例子。

#2


You can use global variables MATLAB:

您可以使用全局变量MATLAB:

1) in the workspace, declare q as global, than set its value

1)在工作空间中,将q声明为全局,而不是设置其值

global q;
q = [1 0 2 4 2 3 4 2 1 0 2];

2) in your function, declare q as global and use it:

2)在你的函数中,将q声明为全局并使用它:

function vector = r(i)
global q;
vect(i) = q(3*i-2:3*i-1);