MATLAB Programming
MATLAB Programming MATLAB Programming
9 Classes and Objects 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” on page 9-49 and “The Asset subsasgn Method” on page 9-47 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 s = stock('XYZ',100,25) You could change the descriptor field with either of the following statements s(1) = 'ABC'; or s.descriptor = 'ABC'; 9-56
Example — Assets and Asset Subclasses See the subsasgn help entry for general information on assignment statements in MATLAB. The Stock display Method When you issue the statement (without terminating with a semicolon) XYZStock = stock('XYZ',100,25) MATLAB looks for a method in the @stock directory called display. The display method for the stock class produces this output. Descriptor: XYZ Date: 17-Nov-1998 Type: stock Current Value: 2500.00 Number of shares: 100 Share price: 25.00 Here is the stock display method. function display(s) % DISPLAY(s) Display a stock object display(s.asset) stg = sprintf('Number of shares: %g\nShare price: %3.2f\n',... s.numShares,s.sharePrice); disp(stg) First, the parent asset object is passed to the asset display method to display its fields (MATLAB calls the asset display method because the input argument is an asset object). The stock object’s fields are displayed in a similar way using a formatted text string. Note that if you did not implement a stock class display method, MATLAB would call the asset display method. This would work, but would display only the descriptor, date, type, and current value. 9-57
- Page 666 and 667: 9 Classes and Objects functions do
- Page 668 and 669: 9 Classes and Objects visible only
- Page 670 and 671: 9 Classes and Objects Class Method
- Page 672 and 673: 9 Classes and Objects Examples of C
- Page 674 and 675: 9 Classes and Objects not support p
- Page 676 and 677: 9 Classes and Objects overloaded me
- Page 678 and 679: 9 Classes and Objects A(1,2).name(3
- Page 680 and 681: 9 Classes and Objects Object Indexi
- Page 682 and 683: 9 Classes and Objects Converter Met
- Page 684 and 685: 9 Classes and Objects Operation M-F
- Page 686 and 687: 9 Classes and Objects Example — A
- Page 688 and 689: 9 Classes and Objects to the .c fie
- Page 690 and 691: 9 Classes and Objects end if d >= 2
- Page 692 and 693: 9 Classes and Objects % SUBSREF swi
- Page 694 and 695: 9 Classes and Objects q = polynom(q
- Page 696 and 697: 9 Classes and Objects % POLYNOM/DIF
- Page 698 and 699: 9 Classes and Objects Building on O
- Page 700 and 701: 9 Classes and Objects Multiple Inhe
- Page 702 and 703: 9 Classes and Objects Inheritance M
- Page 704 and 705: 9 Classes and Objects a.currentValu
- Page 706 and 707: 9 Classes and Objects error('Asset
- Page 708 and 709: 9 Classes and Objects case 'date' a
- Page 710 and 711: 9 Classes and Objects Note that the
- Page 712 and 713: 9 Classes and Objects For example,
- Page 714 and 715: 9 Classes and Objects The Stock sub
- Page 718 and 719: 9 Classes and Objects Example — T
- Page 720 and 721: 9 Classes and Objects if isa(name,'
- Page 722 and 723: 9 Classes and Objects end if saving
- Page 724 and 725: 9 Classes and Objects Saving and Lo
- Page 726 and 727: 9 Classes and Objects • subsref
- Page 728 and 729: 9 Classes and Objects function n =
- Page 730 and 731: 9 Classes and Objects Object Preced
- Page 732 and 733: 9 Classes and Objects How MATLAB De
- Page 734 and 735: 9 Classes and Objects Private funct
- Page 736 and 737: 9 Classes and Objects 9-76
- Page 738 and 739: 10 Scheduling Program Execution wit
- Page 740 and 741: 10 Scheduling Program Execution wit
- Page 742 and 743: 10 Scheduling Program Execution wit
- Page 744 and 745: 10 Scheduling Program Execution wit
- Page 746 and 747: 10 Scheduling Program Execution wit
- Page 748 and 749: 10 Scheduling Program Execution wit
- Page 750 and 751: 10 Scheduling Program Execution wit
- Page 752 and 753: 10 Scheduling Program Execution wit
- Page 754 and 755: 10 Scheduling Program Execution wit
- Page 756 and 757: 10 Scheduling Program Execution wit
- Page 758 and 759: 10 Scheduling Program Execution wit
- Page 760 and 761: 10 Scheduling Program Execution wit
- Page 762 and 763: 11 Improving Performance and Memory
- Page 764 and 765: 11 Improving Performance and Memory
9 Classes and Objects<br />
case '.'<br />
switch index.subs<br />
case 'numShares'<br />
s.numShares = val;<br />
case 'sharePrice'<br />
s.sharePrice = val;<br />
otherwise<br />
s.asset = subsasgn(s.asset,index,val);<br />
end<br />
end<br />
The outer switch statement determines if the index is a numeric or field<br />
name syntax.<br />
The fieldcount asset method determines how many fields there are in the<br />
asset structure and the if statement calls the asset subsasgn method for<br />
indices 1 to fieldcount. See “The Asset fieldcount Method” on page 9-49 and<br />
“The Asset subsasgn Method” on page 9-47 for a description of these methods.<br />
Numeric indices greater than the number returned by fieldcount are<br />
handled by the inner switch statement, which maps the index value to the<br />
appropriate field in the stock structure.<br />
Field name indexing assumes field names other than numShares and<br />
sharePrice are asset fields, which eliminates the need for knowledge of asset<br />
fields by child methods. The asset subsasgn method performs field-name<br />
error checking.<br />
The subsasgn method enables you to assign values to stock object data<br />
structure using two techniques. For example, suppose you have a stock object<br />
s = stock('XYZ',100,25)<br />
You could change the descriptor field with either of the following statements<br />
s(1) = 'ABC';<br />
or<br />
s.descriptor = 'ABC';<br />
9-56