Why does nargout return -1 in this case?
function test
fun=@(a)helper_fun(a,2);
[x,y]=fun(1);
x, % 1
y, % 2
nargout(fun), % -1
end
function [c,d] = helper_fun(a,b)
c=a;
d=b;
end
Is there an alternative to extract the correct number of output variables of fun?
I was hoping to impose a syntax check in a function that takes function_handle as an optional variable and this artifact forced me to either change how my function must look or not check number of outputs.
From the documentation:
nargoutreturns a negative value to mark the position ofvarargoutin the function declaration. As an example, for a function declared asnargoutwill give-2, meaning that the second "output" is actuallyvarargout, which represents a comma-separated list that can contain any number of outputs.nargoutgives-1for anonymous functions because they can return any number of outputs. That is, their signature is equivalent toHow can an anonymous function return more than one output? As indicated here, by delegating the actual work to another function that can. For example:
Compare this to
The result is now
3becausefindis defined with (at most)3outputs.