MATLAB Programming
MATLAB Programming MATLAB Programming
9 Classes and Objects Object Indexing Within Methods If a subscripted reference is made within a class method, MATLAB uses its built-in subsref function to access data within the method’s own class. If the method accesses data from another class, MATLAB calls the overloaded subsref function in that class. The same holds true for subscripted assignment and subsasgn. The following example shows a method, testref, that is defined in the class, employee. Thismethod makes a reference to a field, address, inanobjectof its own class. For this, MATLAB uses the built-in subsref function. It also references the same field in another class, this time using the overloaded subsref of that class. % ---- EMPLOYEE class method: testref.m ---- function testref(myclass,otherclass) myclass.address otherclass.address % use built-in subsref % use overloaded subsref The example creates an employee object and a company object. empl = employee('Johnson','Chicago'); comp = company('The MathWorks','Natick'); The employee class method, testref, is called. MATLAB uses an overloaded subsref only to access data outside of the method’s own class. testref(empl,comp) ans = Chicago ans = Executing @company\subsref ... Natick % built-in subsref was called % @company\subsref was called Defining end Indexing for an Object When you use end in an object indexing expression, MATLAB calls the object’s end class method. If you want to be able to use end in indexing expressions involving objects of your class, you must define an end method for your class. 9-20
Designing User Classes in MATLAB The end method has the calling sequence end(a,k,n) where a is the user object, k is the index in the expression where the end syntax is used, and n is the total number of indices in the expression. Forexample,considertheexpression A(end-1,:) MATLAB calls the end method defined for the object A using the arguments end(A,1,2) That is, the end statement occurs in the first index element and there are two index elements. The class method for end must then return the index value for the last element of the first dimension. When you implement the end method for your class, you must ensure it returns avalueappropriate for the object. Indexing an Object with Another Object When MATLAB encounters an object as an index, it calls the subsindex method defined for the object. For example, suppose you have an object a and you want to use this object to index into another object b. c = b(a); A subsindex method might do something as simple as convert the object to double format to be used as an index, as shown in this sample code. function d = subsindex(a) %SUBSINDEX % convert the object a to double format to be used % as an index in an indexing expression d = double(a); subsindex values are 0-based, not 1-based. 9-21
- Page 630 and 631: 7 Working with Scientific Data Form
- Page 632 and 633: 7 Working with Scientific Data Form
- Page 634 and 635: 7 Working with Scientific Data Form
- Page 636 and 637: 8 Error Handling Checking for Error
- Page 638 and 639: 8 Error Handling Handling and Recov
- Page 640 and 641: 8 Error Handling X = A * B catch di
- Page 642 and 643: 8 Error Handling matrixMultiply(A,
- Page 644 and 645: 8 Error Handling Message Identifier
- Page 646 and 647: 8 Error Handling error('msg_id', 'e
- Page 648 and 649: 8 Error Handling Warnings Like erro
- Page 650 and 651: 8 Error Handling Warning Control Th
- Page 652 and 653: 8 Error Handling warnings by issuin
- Page 654 and 655: 8 Error Handling You must type the
- Page 656 and 657: 8 Error Handling s(2) ans = identif
- Page 658 and 659: 8 Error Handling end function f2(x)
- Page 660 and 661: 8 Error Handling Debugging Errors a
- Page 662 and 663: 9 Classes and Objects Classes and O
- Page 664 and 665: 9 Classes and Objects The diagram s
- 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 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 728 and 729: 9 Classes and Objects function n =
Designing User Classes in <strong>MATLAB</strong><br />
The end method has the calling sequence<br />
end(a,k,n)<br />
where a is the user object, k is the index in the expression where the end<br />
syntax is used, and n is the total number of indices in the expression.<br />
Forexample,considertheexpression<br />
A(end-1,:)<br />
<strong>MATLAB</strong> calls the end method defined for the object A using the arguments<br />
end(A,1,2)<br />
That is, the end statement occurs in the first index element and there are two<br />
index elements. The class method for end must then return the index value for<br />
the last element of the first dimension. When you implement the end method<br />
for your class, you must ensure it returns avalueappropriate for the object.<br />
Indexing an Object with Another Object<br />
When <strong>MATLAB</strong> encounters an object as an index, it calls the subsindex<br />
method defined for the object. For example, suppose you have an object a and<br />
you want to use this object to index into another object b.<br />
c = b(a);<br />
A subsindex method might do something as simple as convert the object to<br />
double format to be used as an index, as shown in this sample code.<br />
function d = subsindex(a)<br />
%SUBSINDEX<br />
% convert the object a to double format to be used<br />
% as an index in an indexing expression<br />
d = double(a);<br />
subsindex values are 0-based, not 1-based.<br />
9-21