11

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.

rienafairefr
  • 364
  • 4
  • 14
  • 1
    take a look at [How do I overload built-in MATLAB functions?](http://www.mathworks.nl/support/solutions/en/data/1-18T0R/index.html?product=ML&solution=1-18T0R) – Gunther Struyf Oct 29 '12 at 09:46

1 Answers1

7

Have you tried this:

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

?

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • Well, what do you know, when matlab knowledge runs dry, there's always some magic syntax to make it work. It works perfectly, Thanks – rienafairefr Oct 17 '12 at 12:46
  • +1 good catch :) This is not limited to classes and member functions. Works for ordinary functions as well. – angainor Oct 17 '12 at 12:47
  • @angainor: Yup. Cell array expansion applied to the cell arrays `varargin` and `varargout` -- it's often overlooked. They really are just ordinary cell arrays :) – Rody Oldenhuis Oct 17 '12 at 12:56