Why is the wrong SUBSASGN function being called when I have an overloaded SUBSASGN function present?

1 view (last 30 days)
I have two custom-made classes and each of them have an overloaded SUBSASGN function defined. Inside one of the SUBSASGN codes, the following line exists:
obj.(s(1).subs) = subsasgn(obj.(s(1).subs), s(2:end), val);
In this statement, there is an explicit call to SUBSASGN. But which SUBSASGN is being called seems to depend on the variable 'val' rather than the object class it is being assigned to. Why is that?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
In a call to an overloaded function, the actual function being called is decided by MATLAB based on the class of the objects in the entire argument list. MATLAB determines which argument has the highest object precedence and the class of
this object controls the method selection. Hence, in this case:
obj.(s(1).subs) = subsasgn(obj.(s(1).subs), s(2:end), val);
the variable 'val' is the dominant object and hence controls the function call. For a detailed explanation of this process and to manipulate the Object Precedence, please refer to the MATLAB documentation on Object Precedence.
You can manipulate the Object Precedence using the INFERIORTO and SUPERIORTO functions. Also, you can enforce the built-in SUBASGN function to be called by using:
obj.(s(1).subs) = builtin('subsasgn', obj.(s(1).subs), s(2:end), val);

More Answers (0)

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Products


Release

R2007a

Community Treasure Hunt

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

Start Hunting!