How to get all outputs (MatLab)?

matlab, output

Solution

Well, thanks to all partisipated in discussion. Summing up, it seems the problem has no general solution, because MatLab itself estimates the number of desired outputs before the function call to use inside it. Three cases can be pointed out though:

1) The funcrion doesn't have `varargout` in definition, thus `nOut=nargout(@fcn)` returns positive number.

Then `nOut` is an actual number of outputs and we can use a cell array and a column list trick.

X=cell(1,nOut);
[X{:}]=fcn(inputs);

2) The funcrion has `varargout` in definition, thus `nOut=nargout(@fcn)` returns negative number. However some correlation with inputs can be found (like `length(varargin)=length(varargout)`).

Then we can calculate the resulting `nOut` from `inputs` and perform the above column list trick.

3) You know the `fcn` developer.

Ask him fot assistance. For example to make the function's output to be a cell array.

Problem

Suppose I have a function that gives out unknown number of output arguments (it depends on input,thus change through the loops). How to get all of them? `nargout` doesn't help as the function uses `varargout` (the result is -1) And of course I can't rewrite the function, otherwise the question wouldn't arise :- )

Original source