MATLAB Programming
MATLAB Programming MATLAB Programming
9 Classes and Objects function n = getAccountNumber(p) % provides a account number for object p n = [upper(p.name(1:3)) strcat(num2str(round(rand(1,7)*10))')']; Note that the portfolio object is indexed by field name, and then by numerical subscript to extract the first three letters. The subsref method must be written to support this form of subscripted reference. The Portfolio subsref Method When MATLAB encounters a subscripted reference, such as that made in the getAccountNumber function p.name(1:3) MATLAB calls the portfolio subsref method to interpret the reference. If you do not define a subsref method, the above statement is undefined for portfolio objects (recall that here p is an object, not just a structure). The portfolio subsref method must support field-name and numeric indexing for the getAccountNumber function to access the portfolio name field. function b = subsref(p,index) % SUBSREF Define field name indexing for portfolio objects switch index(1).type case '.' switch index(1).subs case 'name' if length(index)== 1 b = p.name; else switch index(2).type case '()' b = p.name(index(2).subs{:}); end end end end Note that the portfolio implementation of subsref is designed to provide access to specific elements of the name field; it is not a general implementation 9-68
Example — Defining saveobj and loadobj for Portfolio that provides access to all structure data, such as the stock class implementation of subsref. See the subsref help entry for more information about indexing and objects. 9-69
- 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 716 and 717: 9 Classes and Objects case '.' swit
- 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 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
- Page 766 and 767: 11 Improving Performance and Memory
- Page 768 and 769: 11 Improving Performance and Memory
- Page 770 and 771: 11 Improving Performance and Memory
- Page 772 and 773: 11 Improving Performance and Memory
- Page 774 and 775: 11 Improving Performance and Memory
- Page 776 and 777: 11 Improving Performance and Memory
Example — Defining saveobj and loadobj for Portfolio<br />
that provides access to all structure data, such as the stock class<br />
implementation of subsref.<br />
See the subsref help entry for more information about indexing and objects.<br />
9-69