Is it possible to assign the constant value of a function handle to a variable?
9 次查看(过去 30 天)
显示 更早的评论
Hello,
My program can sometimes generate this function handle:
g = @() 1.0
Or generally: g = @() c , c: constant
Is it possible to transfer that constant into a variable B, so that B = c?
Thanks.
Edit: Is it possible to check whether a function_handle has no inputs(@())?
0 个评论
采纳的回答
Star Strider
2019-2-19
g = @() 1;
B = feval(g)
producing:
B =
1
There may also be other ways to evaluate a function that does not accept an argument.
2 个评论
Star Strider
2019-2-19
As always, my pleasure!
I doubt it. When I tried:
g = @() 1;
B = g()
this also worked, producing:
B =
1
however, this:
B = g(1)
produced:
Too many input arguments.
更多回答(1 个)
Guillaume
2019-2-19
You don't need to use feval to invoke a function handle with no inputs, just use some empty brackets to make it clear you want to call it and not just copy the handle:
f = @() 0.5;
n = f()
f = @() 0.5;
g = @(x) x;
>> nargin(f)
ans =
0
>> nargin(g)
ans =
1
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!