MATLAB: Is the SUBSREF method not called when used from within a method of a class

classMATLABmethodobjectoopoopssubsref

Using the polynomial class example in the documentation, I have created a class @polynom. Within this class, I have defined a number of methods, one of which is SUBSREF. When I call any overloaded method from within a method of the polynom class, I do not run into any problems. MATLAB knows to look in the class for the method first.
I then try to subsref into my object and instead of calling the overloaded SUBSREF, MATLAB attempts to call the built-in SUBSREF. To work around this I have to explicity call SUBSREF. I would like to know why this works for all other overloaded methods.
The following is an example of calling an object's subsref from the command line:
>> p = polynom([1 2 3 4])
p =
x^3 + 2*x^2 + 3*x + 4
>> x = 2
x =
2
>> y=p(x)
y =
26
When calling y = p(x) from an overloaded method called plot:
>> plot(p)
Warning: Subscript indices must be integer values.
> In p:\@polynom\plot.m at line 10
??? Index into matrix is negative or zero. See release notes on changes to
logical indices.
Error in ==> p:\@polynom\plot.m
On line 10 ==> y = p(x);
To workaround this, I have to call SUBSREF explicitly:
y = polyval(p,x);
subs.type = '()';
subs.subs = {x};
y = subsref(p, subs); %should call subsref here

Best Answer

This is working as it was designed. The documentation was fixed to explain this behavior in MATLAB 7.1 (R14SP3). For a detailed explanation or if you are using versions before MATLAB 7.1, read below:
Whenever a class method requires the functionality of the overloaded SUBSREF, it must call the overloaded SUBSREF using the function call to SUBSREF rather than using SUBSREF operators like '()', '{}', '.' etc.
Although this behavior may appear inconsistent when compared to other methods, it is in fact provided in order to prevent erroneously entering a state where the the SUBSREF method ends up calling itself thereby ending up in an endless loop.
The same is true for SUBSASGN.