I have a function that returns three values [A,B,C]=ABC(x).
Is it possible to define a function A(x) in an elegant way such that returns the first value of ABC(x), B(x) for the second value, etc.?
Thanks
As you wrote your function ([A,B,C]=ABC(x)) it does not return a vector per say, it returns 3 values.
If you call your function like this
a = ABC(x)
a would be equal to A.
EDIT :
function b = B(x)
[~, b, ~] = ABC(x)
end
~ notation was introduced with R2009. See for example stackoverflow.com/a/747439/1967396You could include a 2nd input argument if it is acceptable to you. You could use varargin to accept variable number of input arguments.
function outValue=ABC(varargin);
if nargin==0
error('Need at least one argument');
elseif nargin==1
%obtain result
outValue=result;
elseif nargin==2
%obtain result
outValue=result(index);
else
error('Function accepts maximum of 2 arguments');
end
end