Passing multiple outputs inside a function

조회 수: 2 (최근 30일)
Victor
Victor 2011년 9월 8일
I have been trying to program a function that will evaluate another function:
function [result]=evaluate(second_function,x,y,z)
.
.
.
the problem arises when the second_function has multiple outputs:
function [a,b,c]=second_function(x,yz)
I am clueless as to how to pass each output to the calling function ("evaluate"), so that it can use them for further purposes. I am aware of the nargout function but then I am lost as to how to dynamically assign each output to, say, a vector containing each output in a different row:
function [result]=evaluate(second_function,x,y,z)
v(:)=second_function(x,y,z);
Of course, I could program second_function in a way that the multiple outputs would be compressed in a vector:
function [a]=second_function(x,y,z)
a(1)=...;
a(2)=...;
a(3)=...;
But that adds further complications to the rest of my code.
I have to clarify that second_function is different each time (user defined) and I want the program to dynamically evaluate it.

채택된 답변

Walter Roberson
Walter Roberson 2011년 9월 8일
You cannot do this unless you are willing to restrict the user functions to not using varargout .
If you are willing to restrict to not using varargout, then you can use
clear out
[out{1:nargout(second_function)}] = second_function(x,y,z);
and then pass out{:} down the line.
However, if the user is allowed to use varargout, then they could easily write a function that returns as many outputs as there were output slots provided. Such a function already exists, by the way: deal() will return as many outputs as you give output locations.
  댓글 수: 1
Victor
Victor 2011년 9월 9일
Spot on!
[out{1:nargout(second_function)}] = second_function(x,y,z)
it really worked!
I would like to avoid varargout since I believe that it is easier for the user, who I assume doesn't necessarily know how it works. Also, I believe that having several separate outputs makes my work easier as I can analyze the input function with nargin and nargout (nargout yields -nargout when used with a varargout function and is "blind" to how many variables are nested in varargout: I need to have a full understanding of the target, user-defined, function).
I digress, thanks for the help!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Paulo Silva
Paulo Silva 2011년 9월 8일
  댓글 수: 2
Victor
Victor 2011년 9월 8일
Sorry if I am being thick, but *varargin* is for "flexible" inputs, right?
Maybe I didn't formulate my question clearly enough. What I'm trying to do is to pass the *outputs* from any given function to a variable in my function.
The number of outputs could be anything. They are user defined. My function needs each and every output in order to perform further calculations.
Thanks anyway!
Oleg Komarov
Oleg Komarov 2011년 9월 8일
You can use varargout and store several outputs in a cell array and pass it to the second functions as out{:}, i.e. "unpacking" the content of the cell array.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by