MATLAB Object-Oriented Programming

(Joyce) #1

  • If A does not exist before you execute the assignment statement, MATLAB creates a
    struct variable, A with field Name and assigns the value of B to this field location.

  • If struct A exists, but has no field Name, then MATLAB adds the field Name and
    assigns the value of B to the new field location.

  • If struct A exists and has a Name field, then MATLAB assigns the value of B to Name.


You can redefine all or some of these assignment behaviors by implementing a subsasgn
method for your class.


Indexed Assignment to Objects


If A is an object, this expression:


A.Name = B


Calls A = subsasgn(A,S,B) where, S has these values:


S.type = '.'
S.subs = 'Name'


The default subsasgn:



  • Attempts to assign B to the Name property.

  • If the class of A does not have a Name property, MATLAB returns an error.

  • If the Name property has restricted access (private or protected), MATLAB
    determines if the assignment is allowed based on the context in which the assignment
    is made.

  • If the class of A defines a set method for property Name, MATLAB calls the set method.

  • MATLAB applies all other property attributes before determining whether to assigning
    B to the property Name.


Compound Indexed Assignments


These simple calls are combined for more complicated indexing expressions. In such
cases, length(S) is the number of indexing levels. For example,


A(1,2).PropertyName(1:4) = B


calls subsasgn(A,S,B), where S is a 3-by-1 array of structures with the values:


Indexed Assignment
Free download pdf