MATLAB: Is the wrong SUBSASGN function being called when I have an overloaded SUBSASGN function present

bult-inMATLABobjectoverloadedprecedencesubsasgnwong

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?

Best Answer

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);