Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all outputs (MatLab)?

Tags:

output

matlab

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 :- )

like image 309
Noxobar Avatar asked Feb 13 '23 13:02

Noxobar


2 Answers

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.

like image 116
Noxobar Avatar answered Feb 15 '23 10:02

Noxobar


One of ways I usually use in this case is to store all outputs in a cell array inside the function. Getting the cell array outside the function's body, you might investigate its length and other properties.

like image 31
freude Avatar answered Feb 15 '23 09:02

freude