从变量读取的可变长度MATLAB参数

时间:2021-06-23 00:28:37

I have a function with variable arguments, declared in the standard way:

我有一个带有变量参数的函数,以标准方式声明:

[] = foo ( varargin )

and I would like to call it from another function, but specify the arguments programmatically. My best attempt is something like the following:

我想从另一个函数调用它,但以编程方式指定参数。我最好的尝试类似于以下内容:

% bar isn't populated like this, but this is how it ends up
bar = { 'var1' 'var2' 'var3' }; 
foo( bar );

However, bar is put into a 1x1 cell array, and not interpreted as a 1x3 cell array as I intended. I can't change foo, so is there a workaround?

但是,bar被放入1x1单元阵列中,并不像我预期的那样被解释为1x3单元阵列。我无法改变foo,所以有解决方法吗?

2 个解决方案

#1


If you have variables a, b, and c that you want to collect together somewhere and ultimately pass to a function as a series of inputs, you can do the following:

如果您想要在某处收集变量a,b和c并最终将其作为一系列输入传递给函数,则可以执行以下操作:

inArgs = {a b c};  % Put values in a cell array
foo(inArgs{:});

The syntax inArgs{:} extracts all the values from the cell array as a comma-separated list. The above is therefore equivalent to this:

inArgs {:}中的语法将单元格数组中的所有值作为逗号分隔列表提取。因此,上述内容相当于:

foo(a,b,c);

If foo is written to accept a variable-length argument list, then the varargin variable will end up being a 1-by-3 cell array where each element stores a separate input argument. Basically, varargin will look exactly like the variable inArgs. If your call to foo didn't use the {:} operator:

如果写入foo以接受可变长度参数列表,则varargin变量将最终成为1乘3的单元数组,其中每个元素存储单独的输入参数。基本上,varargin看起来与theArgs中的变量完全相同。如果你对foo的调用没有使用{:}运算符:

foo(inArgs);

then the varargin variable would be a 1-by-1 cell array where the first element is itself the cell array inArgs. In other words, foo would have only 1 input (a 1-by-3 cell array).

那么varargin变量将是1乘1的单元阵列,其中第一个元素本身就是Args中的单元阵列。换句话说,foo只有1个输入(1乘3单元阵列)。

#2


The only way that I'm aware of is to use eval, however I don't have MATLAB here, so I can't check the syntax correctly.

我知道的唯一方法是使用eval,但是我没有MATLAB,所以我无法正确检查语法。

If you coerce the bar into a string of the form "'var1', 'var2', 'var3'", you can do:

如果你将该栏强制转换为“'var1','var2','var3'”形式的字符串,你可以这样做:

eval(["foo(", barString, ")"])

Hope that gets you going and sorry it isn't a comprehensive answer.

希望能让你前进并抱歉这不是一个全面的答案。

#1


If you have variables a, b, and c that you want to collect together somewhere and ultimately pass to a function as a series of inputs, you can do the following:

如果您想要在某处收集变量a,b和c并最终将其作为一系列输入传递给函数,则可以执行以下操作:

inArgs = {a b c};  % Put values in a cell array
foo(inArgs{:});

The syntax inArgs{:} extracts all the values from the cell array as a comma-separated list. The above is therefore equivalent to this:

inArgs {:}中的语法将单元格数组中的所有值作为逗号分隔列表提取。因此,上述内容相当于:

foo(a,b,c);

If foo is written to accept a variable-length argument list, then the varargin variable will end up being a 1-by-3 cell array where each element stores a separate input argument. Basically, varargin will look exactly like the variable inArgs. If your call to foo didn't use the {:} operator:

如果写入foo以接受可变长度参数列表,则varargin变量将最终成为1乘3的单元数组,其中每个元素存储单独的输入参数。基本上,varargin看起来与theArgs中的变量完全相同。如果你对foo的调用没有使用{:}运算符:

foo(inArgs);

then the varargin variable would be a 1-by-1 cell array where the first element is itself the cell array inArgs. In other words, foo would have only 1 input (a 1-by-3 cell array).

那么varargin变量将是1乘1的单元阵列,其中第一个元素本身就是Args中的单元阵列。换句话说,foo只有1个输入(1乘3单元阵列)。

#2


The only way that I'm aware of is to use eval, however I don't have MATLAB here, so I can't check the syntax correctly.

我知道的唯一方法是使用eval,但是我没有MATLAB,所以我无法正确检查语法。

If you coerce the bar into a string of the form "'var1', 'var2', 'var3'", you can do:

如果你将该栏强制转换为“'var1','var2','var3'”形式的字符串,你可以这样做:

eval(["foo(", barString, ")"])

Hope that gets you going and sorry it isn't a comprehensive answer.

希望能让你前进并抱歉这不是一个全面的答案。