Overloading Numel

I have a need to overload numel for a data container class that i'm building. However it seems to break the overloaded subsref method because apparently subsref is calling numel (invisibly too, for all the breakpoints that i put in my overloaded numel never got triggered when calling subsref).
Please provide a more comprehensive documentation in overloading numel properly for custom object classes and how it is called with respect to subsref.

4 Comments

Looking around, I see examples in which people found it made sense to use numel within their subsref; see for example
http://www.mathworks.com/matlabcentral/answers/6764-overloading-subsref
Thomas Lai
Thomas Lai on 7 Jun 2012
Thank you, walter for the link but I'm afraid the problem that I'm having is more to do with Matlab's hidden mechanisms of preprocessing the function call before it even reaches subsref itself. I overloaded the numel method to give a proper output for my data container object but that screws up with subsref because apparently matlab calls numel to calculate nargout for the subsref method under the hood and then it goes around complaining that my subsref function is giving the wrong number of output data.
I also found this in the matlab documentation for numel:
"It is important to note the significance of numel with regards to the overloaded subsref and subsasgn functions. In the case of the overloaded subsref function for brace and dot indexing (as described in the last paragraph), numel is used to compute the number of expected outputs (nargout) returned from subsref. For the overloaded subsasgn function, numel is used to compute the number of expected inputs (nargin) to be assigned using subsasgn. The nargin value for the overloaded subsasgn function is the value returned by numel plus 2 (one for the variable being assigned to, and one for the structure array of subscripts).
As a class designer, you must ensure that the value of n returned by the built-in numel function is consistent with the class design for that object. If n is different from either the nargout for the overloaded subsref function or the nargin for the overloaded subsasgn function, then you need to overload numel to return a value of n that is consistent with the class' subsref and subsasgn functions. Otherwise, MATLAB produces errors when calling these functions."
But it never did specify how exactly is numel doing it. I need to know what numel has to do with subsref and nargout and what are the circumstances when it was being called during the hidden subsref preroutine.
Daniel Shub
Daniel Shub on 8 Jun 2012
The documentation seems pretty extensive. Can you provide some sample code.
Thomas Lai
Thomas Lai on 11 Jun 2012
I did not have a numel function because I did not know how to implement it in such a way as to provide the correct nargout values for subsref.

Sign in to comment.

Answers (0)

Categories

Products

Asked:

on 7 Jun 2012

Community Treasure Hunt

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

Start Hunting!