Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function calling variable output number

I've found myself trying to interface a custom class with builtin functions, and I've encoutered a situation I could only solve with eval, I'd like a "cleaner" way.

Basically, the builtin function is defined as varargout=blabla(varargin) I defined an overriden function in the custom class, as varargout=blabla(varargin). The function looks like:

function varargout=blabla(varargin)
    varargout=blabla(function_of_varargin)
end

The function of varargin transforms it from the custom class to the builtin clas.

But it doesn't work as-is: Basically when the builtin function is called inside the overriden function, it sees only one output parameter (varargout), even if the custom overriden function is called with more than one output parameter.

I solved it by basically calling this :

[varargout{1},varargout{2},...,varargout{nargout}]=blabla(function_of_varargin)

Constructing the LHS with a loop and eval-ing.

like image 993
rienafairefr Avatar asked Oct 17 '12 12:10

rienafairefr


People also ask

What variable is the output of a function?

For a function, the input is called the independent variable , and the output is called the dependent variable .

Can a function have variable number of parameters?

To call a function with a variable number of arguments, simply specify any number of arguments in the function call. An example is the printf function from the C run-time library. The function call must include one argument for each type name declared in the parameter list or the list of argument types.

Can a function have variable number of parameters in Python?

Yes. You can use *args as a non-keyword argument. You will then be able to pass any number of arguments. As you can see, Python will unpack the arguments as a single tuple with all the arguments.

How do you return a value from a function?

To return a value from a function, you must include a return statement, followed by the value to be returned, before the function's end statement. If you do not include a return statement or if you do not specify a value after the keyword return, the value returned by the function is unpredictable.


1 Answers

Have you tried this:

[varargout{1:nargout}] = blabla(varargin{:})

?

like image 142
Rody Oldenhuis Avatar answered Oct 16 '22 17:10

Rody Oldenhuis