Is it possible to reference an array of inline functions or function handles created within a structure in MATLAB?
2 次查看(过去 30 天)
显示 更早的评论
Is it possible to reference an array of inline functions or function handles created within a structure in MATLAB?
The following example demonstrates this problem:
f1 = inline('sin(x)');
f2 = inline('cos(x)');
h.f(1) = f1;
I now calculate
h.f(pi/2)
ans =
1
But if I define:
h.f(2) = f2
I get
h =
f: [1x2 inline]
Now I try to calculate inline results:
h.f([pi/2;pi/2])
I receive the error
??? Comma separated list must have exactly one item.
How can I reference the index of the vector of the inline functions, as well as pass an argument?
采纳的回答
MathWorks Support Team
2009-6-27
You should not create the vector of inline functions. When you do this without a structure, you receive an error.
f1 = inline('sin(x)');
f2 = inline('cos(x)');
f(1) = f1;
f(2) = f2
??? Comma separated list must have exactly one item.
To work around this error, rather than creating a vector of inline functions, you should create a cell array. Here is an example:
f1 = inline('sin(x)');
f2 = inline('cos(x)');
h.f{1} = f1;
h.f{2} = f2;
h.f{2}(pi/4),
This produces the correct result:
ans =
0.7071
You can also do this with function handles
f{1}=@sin;
f{2}=@cos;
feval(f{1},1);
end
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!