Say I have a function foo
that can return three values given an input:
function [a,b,c] = foo(input)
The calculations of variables b
and c
take a long time, so sometimes I may wish to ignore their calculation within foo
. If I want to ignore both calculations, I simply call the function like this:
output1 = foo(input);
and then include nargout
within foo
:
if nargout == 1
% Code to calculate "a" only
else
% Code to calculate other variables
The problem occurs if I want to calculate the last output, but not the second. In that case my function call would be:
[output1,~,output3] = foo(input);
Now if I use nargout
within foo
to check how many outputs are in the function-call, it will always return 3
because the tilde operator (~
) is considered a valid output. Therefore, I cannot use nargout
to determine whether or not to calculate the second output, b
, within foo
.
Is there any other way to do this? I.e., is it possible to check which outputs of a function-call are discarded from within the function itself?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…