| Programming and Data Types | ![]() |
The Asset subsasgn Method
The subsasgn method is the assignment equivalent of the subsref method. This version enables you to change the data contained in an object using one-based numeric indexing and structure field name indexing. The outer switch statement determines if the index is a numeric or field name syntax. The inner switch statements map the index value to the appropriate value in the stock structure.
MATLAB calls subsasgn whenever you execute an assignment statement (e.g., A(i) = val, A{i} = val, or A.fieldname = val).
function a = subsasgn(a,index,val)
% SUBSASGN Define index assignment for asset objects
switch index.type
case '()'
switch index.subs{:}
case 1
a.descriptor = val;
case 2
a.date = val;
case 3
a.current_value = val;
otherwise
error('Index out of range')
end
case '.'
switch index.subs
case 'descriptor'
a.descriptor = val;
case 'date'
a.date = val;
case 'current_value'
a.current_value = val;
otherwise
error('Invalid field name')
end
end
The subsasgn method enables you to assign values to the asset object data structure using two techniques. For example, suppose you have a child stock object s.
s = stock('XYZ',100,25);
Within stock class methods, you could change the descriptor field with either of the following statements
s.asset(1) = 'ABC';
s.asset.descriptor = 'ABC';
See the The Stock subsasgn Method for an example of how the child subsasgn method calls the parent subsasgn method.
| The Asset subsref Method | The Asset display Method | ![]() |