0

If I have a function:

function [out1,out2,...] = functionName[in1,in2]
function code here
end

And another function

function[newout1,newout2] = newfunctionName[in1,in2]
[newout1]=out1+out2;
[newout2]=out2+out3;

How do I go about calling the various outputs, out1, out2, out3 etc...

  • 1
    Possible duplicate? http://stackoverflow.com/q/39359410/6579744 – rahnema1 Dec 06 '16 at 17:24
  • 2
    You call `functionName` from inside `newfunctionName` and get the outputs. I'm really not sure what the question is here. – beaker Dec 06 '16 at 17:28
  • @beaker If `newfunctionName` is recursive, and say `if n=1 newout1=5; newout2=10; else [newout1,newout2]=functionName(newfunctionName(n/2),**out2**)` how would I get the `out2` – Anon21304982 Dec 06 '16 at 20:36
  • Please add clarifications to the question itself by editing; code in comments is impossible to read and the question should be complete and self-contained. – beaker Dec 06 '16 at 20:38

1 Answers1

0

I don't understand very well what you meant by "calling the various outputs, out1, out2, out3 etc" but to answer the title's question,

In order to access the nth output of any function you would have to first call nargout on that function's name and then insert its output into a cell of preallocated size. Sample code below,

n = 5;
nout = abs(nargout('functionName'));
if n>nout
    error(['n must be lower or equal than ',num2str(nout)])
end
out = cell(1,n);
[out{:}] = functionName(in1,in2);
nth_output = out{n};

This could be done inside any function that has functionName in its path.

lucianopaz
  • 1,212
  • 10
  • 17