Is it possible to assign the constant value of a function handle to a variable?
조회 수: 6 (최근 30일)
이전 댓글 표시
Panagiotis Panagopoulos Papageorgiou
2019년 2월 19일
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에서 Programming Utilities에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!