How to get all outputs (MatLab)?

Suppose I have a function that returns an unknown number of output arguments (it depends on the input, so it changes through loops). How to get all of them?

nargoutdoesn't help as function uses varargout(result is -1)

And, of course, I cannot rewrite the function, otherwise the question will not arise :-)

+4
source share
3 answers

Well, thanks to everyone that was in the discussion. To summarize, it seems that the problem has no general solution, because MatLab itself estimates the number of desired outputs before calling the function for use inside it. Three cases can be noted:

1) varargout , nOut=nargout(@fcn) .

nOut - , .

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

2) varargout , nOut=nargout(@fcn) . (, length(varargin)=length(varargout)).

nOut inputs .

3) fcn.

. , .

+6

, , . , .

+2

Here's how you could handle the whole problem. I have not mentioned this decision before because ... it is terrible.

Suppose a function can have 1 or 2 output arguments:

try 
  [a, b] = f(x)
catch
  a = f(x)
end

Of course, this can be done for any number of output arguments, but you really do not want this.

0
source

Source: https://habr.com/ru/post/1547769/


All Articles