问题描述
我正在使用符号工具箱来生成matlab函数.但是生成函数的输入数量随我需要的对象数量(例如,开关数量)而变化.对于2和3开关,生成的函数如下所示:
I am using symbolic toolbox to generate a matlab function. But the number of input to the generated function is varying with the number of objects that I need (e.g., number of switches). For 2 and 3 switches the generated function look likes this :
y = fun(a1,a2,b1,b2)
y = fun(a1,a2,a3,b1,b2,b3)
在使用此功能的脚本中,我建立了这些参数的向量:
In the script using this function I establish vectors of these parameters:
a = [a1 a2 ...]
我想要的是直接调用生成的函数或创建包装函数,以便在更改开关数量时不需要更改call语句.使这个问题更加复杂的是,这些变量是ACADO变量.这意味着不允许进行矩阵和逐元素运算(即,所有数学运算都必须使用标量完成,并且必须在符号工具箱中的方程式中标明标量).
What I want is to either call the generated function directly or make a wrapper function, so that I do not need to change the call statement when I change the number of switches. To complicate this problem even more, these variables are ACADO variables. That means that matrix and element-wise operation is not allowed (i.e., all math operation must be done with scalars, and equations in symbolic toolbox must be written for scalars).
推荐答案
您可能会寻找单元格数组和{:}
运算符.它将单元格的内容更改为逗号分隔的列表.结果可以作为参数传递给函数.例如:
You probably look for cell arrays and the {:}
operator. It changes the contents of the cell to a coma separated list. The result can be passed to a function as parameters. For example:
v2 = {a1, a2, b1, b2};
v3 = {a1, a2, a3, b1, b2, b3};
还有一个示例函数:
function fun(varargin)
display(['number of parameters: ' num2str(nargin)]);
您可以按如下所示透明"地调用不同数量的参数的函数
You can call the function for different number of parameters 'transparently' as follows
fun(v2{:})
number of parameters: 4
fun(v3{:})
number of parameters: 6
这篇关于在Matlab中使用不同数量的参数调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!