| Programming | ![]() |
The Stock subsasgn Method
The subsasgn method enables you to change the data contained in a stock object using numeric indexing and structure field name indexing. MATLAB calls subsasgn whenever you execute an assignment statement
(e.g., A(i) = val, A{i} = val, or A.fieldname = val).
function s = subsasgn(s,index,val)
% SUBSASGN Define index assignment for stock objects
fc = fieldcount(s.asset);
switch index.type
case '()'
if (index.subs{:} <= fc)
s.asset = subsasgn(s.asset,index,val);
else
switch index.subs{:}-fc
case 1
s.numShares = val;
case 2
s.sharePrice = val;
otherwise
error(['Index must be in the range 1 to ',num2str(fc + 2)])
end
end
case '.'
switch index.subs
case 'numShares'
s.numShares = val;
case 'sharePrice'
s.sharePrice = val;
otherwise
s.asset = subsasgn(s.asset,index,val);
end
end
The outer switch statement determines if the index is a numeric or field name syntax.
The fieldcount asset method determines how many fields there are in the asset structure and the if statement calls the asset subsasgn method for indices 1 to fieldcount. See The Asset fieldcount Method and The Asset subsasgn Method for a description of these methods.
Numeric indices greater than the number returned by fieldcount are handled by the inner switch statement, which maps the index value to the appropriate field in the stock structure.
Field-name indexing assumes field names other than numShares and sharePrice are asset fields, which eliminates the need for knowledge of asset fields by child methods. The asset subsasgn method performs field-name error checking.
The subsasgn method enables you to assign values to stock object data structure using two techniques. For example, suppose you have a stock object
You could change the descriptor field with either of the following statements
See the subsasgn help entry for general information on assignment statements in MATLAB.
| The Stock subsref Method | The Stock display Method | ![]() |
© 1994-2005 The MathWorks, Inc.