@ReferAssignGetSet February 21, me. berkeley

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
Foundations of (Theoretical) Computer Science
MATLAB and Simulinklecture 31 To days Outline  Functions  Strings  Sparse Arrays  Cell Arrays  Structures  Exercises on this days topics.
Functions UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Numerical Integration UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
Fall 2006AE6382 Design Computing1 OOP: Creating a Class More OOP concepts An example that creates a ASSET class and shows how it might be used Extend the.
Simulink SubSystems and Masking April 22, Copyright , Andy Packard. This work is licensed under the.
Cell Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Recursion and Induction UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
Digital Image Processing Lecture10: MATLAB Example – Utility M-functions for Intensity Transformations.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Sorting UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Script Files UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Objects and Classes UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Numerical Differentiation UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under.
Searching UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Polynomials UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
General Computer Science for Engineers CISC 106 Lecture 2^4 Roger Craig Computer and Information Sciences 03/23/2009.
Intro to Simulink April 15, Copyright , Andy Packard. This work is licensed under the Creative Commons.
Inheritance in Matlab Classes E177 February 14, Copyright , Andy Packard. This work is licensed.
Root Finding UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Fall 2006AE6382 Design Computing1 OOP Programming in Matlab Review our ASSET class and study how it might be used as a parent for other classes Extend.
Get/Set Methods UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Intro to Simulink Modified by Gary Balas 20 Feb 2011 Copyright , Andy Packard. This work is licensed under.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
CS0004: Introduction to Programming
Arrays Chapter 7.
Visual Basic.NET Windows Programming
Black Box Software Testing Spring 2005
OOP: Creating a Class Topics More OOP concepts
FAST TRACKING IN THE IEEE
CS 5010 Program Design Paradigms “Bootcamp” Lesson 5.2
Introduction to Java E177 April 29, me. berkeley
Black Box Software Testing Spring 2005
FUNCTIONS In C++.
Agenda Warmup AP Exam Review: Litvin A2
For/Switch/While/Try UC Berkeley Fall 2004, E77 me
ME190L Nyquist Stability Criterion UC Berkeley Fall
Creating Your OwnClasses
Scripts & Functions Scripts and functions are contained in .m-files
“Clumsy mixture of ingredients…”
Reusable Graphics Objects UC Berkeley Fall 2004, E77 me
User Defined Functions
Lecture 4: Interface Design
Class Info E177 January 22, me. berkeley
Topics Introduction to File Input and Output
E177, Graphics Objects in Matlab February 26, me
Object-Oriented Programming Using C++ Second Edition
Selection Statements Chapter 4 Attaway MATLAB 4E.
Black Box Software Testing Fall 2004
Objects with Functions and Arrays
Black Box Software Testing Fall 2005 Overview – Part 1 of 3
Black Box Software Testing 2004 Academic Edition
Logical Operations In Matlab.
MATLAB Programming Indexing Copyright © Software Carpentry 2011
Class Intro/TDD Intro 8/23/2005
Nested Loops & The Step Parameter
E177, Reference/Assignment for Classes: Intro February 12, Copyright , Andy Packard. This work.
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Variables in C Topics Naming Variables Declaring Variables
ENERGY 211 / CME 211 Lecture 27 November 21, 2008.
Topics Introduction to File Input and Output
Arrays 3/4 June 3, 2019 ICS102: The course.
Selection Statements Chapter 4 Attaway MATLAB 5E.
Basics of Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Presentation transcript:

@ReferAssignGetSet February 21, 2008 http://jagger. me. berkeley @ReferAssignGetSet February 21, 2008 http://jagger.me.berkeley.edu/~pack/e177 Copyright 2005-2008, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

@ReferAssignGetSet Many ideas of get, set, subsref, subsasgn will to carry over to all classes we write. For example, Reference of M.Value is the same as get(M,’Value’) Multi-depth subsref has specific recursive meaning Assignment of M.Value=B is the same as set(M,’Value’,B) Multi-depth subsasgn has specific recursive meaning In some cases, () reference and assignment should be handled by the built-in methods for Matlab arrays set should work with or without output arguments Special Matlab-like conventions for get and set should be followed Calls with 1-input argument Property names can be partially specified Multiple args in set(A,P1,V1,P2,V2) This is a good motivation to write a class that handles all of this functionality, and all other classes will simply inherit this.

@ReferAssignGetSet Questions/Options for a given class Is property reference allowed, as in Object.Property Is property assignment allowed, as in Object.Property = Value Is () reference allowed, as in Object(Idx1,Idx2) if so, are builtin methods used, or does the class programmer supply the reference method? Is () assignment allowed, as in Object(Idx1,Idx2) = Value if so, are builtin methods used, or does the class programmer supply the assignment method? Is {} reference allowed, as in Object{Idx1,Idx2} Is {} assignment allowed, as in Object{Idx1,Idx2} = Value Does set use assignin when called with no output arguments? set(A,Prop,Value) %uses inputname, overwrites A A = set(A,Prop,Value) % overwrites A, directly B = set(A,Prop,Value) % A unchanged

@ReferAssignGetSet Constructor (see actual code) just keeps track of the options to handle decisions for each of the cases described. function A = ReferAssignGetSet(InputArgsSpecifiers) A.PropertyRef = 'followGet'; % or 'disallow' A.PropertyAssign = 'followSet'; % 'disallow' A.ParenRef = 'ProgSupplied'; % 'builtin', 'disallow' A.ParenAssign = 'ProgSupplied'; % 'builtin', 'disallow' A.BraceRef = 'ProgSupplied'; % 'builtin', 'disallow' A.BraceAssign = 'ProgSupplied'; % 'builtin', 'disallow' A.AllowSetAssignin = 0; % or 1 A = class(A,'ReferAssignGetSet'); Also need general purpose versions of get, set, subsref, subsasgn

Folder layout @ReferAssignGetSet @class1 @class2 @class3 private ReferAssignGetSet.m get.m set.m subsref.m subsasgn.m fieldnames.m class1.m GetSetPropertyInfo isvalid PrivateGet PrivateSet ParenthesisReference ParenthesisAssign BraceReference BraceAssign horzcat, vertcat smartpropmatch private @ReferAssignGetSet class2.m GetSetPropertyInfo Isvalid PrivateGet PrivateSet ParenthesisReference ParenthesisAssign OtherMethodsforclass2 @class1 class3.m GetSetPropertyInfo Isvalid PrivateGet PrivateSet OtherMethodsforclass3 @class2 @class3

Example child class: @point function A = point(X,Y,varargin) S.X = X; S.Y = Y; % point in 2-dim Euclidean space PropRef = 'followGet'; % or 'disallow' PropAssign = 'followSet'; % or 'disallow' ParenRef = 'builtin'; % or 'builtin' or 'disallow' ParenAssign = 'builtin'; % or 'builtin' or 'disallow' BraceRef = 'disallow'; % or 'ProgSupplied' or 'builtin' BraceAssign = 'disallow'; %or 'ProgSupplied‘, 'builtin' ASA = 1; % or 0, ASA-> AllowSetAssignin B = ReferAssignGetSet(PropRef,PropAssign,... ParenRef,ParenAssign,BraceRef,BraceAssign,ASA); A = class(S,'point',B); lvin = length(varargin); if lvin>0 && ceil(lvin/2)==floor(lvin/2) A = set(A,varargin{:}); end

@ReferAssignGetSet get and set call the child’s GetSetPropertyInfo function [GPN,SPN,SPD] = GetSetPropertyInfo(A) % Return a N-by-1 cell array of GET Prop Names % Return a M-by-1 cell array of SET Prop Names % Return a M-by-1 cell array of SET Property % Descriptions get & set also call child’s PrivateGet and PrivateSet function Value = PrivateGet(Object,GPropName) function B = PrivateSet(Object,SPropName,Value)

@point/GetSetPropertyInfo.m function [GPN,SPN,SPD] = GetSetPropertyInfo(A) CommonNames = {'XCoord';'YCoord'}; OnlyGet = {'Radius';'Angle'}; OnlySet = cell(0,1); % empty cell GPN = [CommonNames;OnlyGet]; % gettable properties SPN = [CommonNames;OnlySet]; % settable properties SPD = {'XCoordinate of Point';'YCoordinate of Point'};

Pseudocode for get.m function V = get(A,Property) [GPN,SPN,SPD] = GetSetPropertyInfo(A); if nargin==2 FullPropName = smartpropmatch(Property,GPN); V = PrivateGet(A,FullPropName); else % nargin==1 % Create structure V1 with fieldnames equal % to the property names in GPN. Then fill % the values of the struct with the values % calling PrivateGet. if nargout==1; V = V1; else; disp(V1) end In @ReferAssignGetSet/private

Pseudocode for PrivateGet.m function V = PrivateGet(A,FullPropName) switch FullPropName case PropertyName1 V = % extract Property1 from fields of A case PropertyName2 V = % extract Property2 from fields of A case ... case PropertyNameK V = % extract PropertyK from fields of A Otherwise error(’Unknown Property’); end

@point/PrivateGet.m function V = PrivateGet(A,FullPropName) switch FullPropName case 'XCoord' V = A.X; case 'YCoord' V = A.Y; case 'Radius' V = norm([A.X A.Y]); case 'Angle' V = atan2(A.Y,A.X); otherwise error([FullPropName ': Unknown Property.']); end

@point/PrivateSet.m function A = PrivateSet(A,FullPropName,Value) switch FullPropName case 'XCoord' if isa(Value,'double') && isscalar(Value) A.X = Value; else error('XCoordinate should be real scalar'); end case 'YCoord' A.Y = Value; error('YCoordinate should be real scalar'); otherwise error([FullPropName ': Unknown Property']);

Similar for Assignment/Reference General purpose subsref, subsasgn reside in @GetSetReferAssign they call the object’s specific reference and assignment methods ParenthesisAssign ParenthesisReference BraceAssign BraceReference In this way, one version of subsref and subsasgn address the issues Proper relation to get/set Deep references/assignments Similarly, there should be tight integration with horzcat and vertcat, but at this point, we leave that to the programmer...

@ReferAssignGetSet/subsref function B = subsref(A,L) ReAsGeSe = A(1); switch L(1).type case '.' if isequal(ReAsGeSe.PropertyRef,'followGet') B = get(A,L(1).subs); else error('dot(.) reference not allowed. Use GET.'); end case '()' case '{}' if length(L)>1 B = subsref(B,L(2:end));

@ReferAssignGetSet/subsref function B = subsref(A,L) ReAsGeSe = A(1); switch L(1).type case '.' case '()' if isequal(ReAsGeSe.ParenRef,'builtin') B = builtin('subsref',A,L(1)); elseif isequal(ReAsGeSe.ParenRef,'ProgSupplied') B = ParenthesisReference(A,L(1).subs); else error('() reference is not allowed.'); end case '{}' if length(L)>1 B = subsref(B,L(2:end));

@ReferAssignGetSet/subsref function B = subsref(A,L) ReAsGeSe = A(1); switch L(1).type case '.' case '()' case '{}' if isequal(ReAsGeSe.BraceRef,'builtin') B = builtin('subsref',A,L(1)); elseif isequal(ReAsGeSe.BraceRef,'ProgSupplied') B = BraceReference(A,L(1).subs); else error('{} reference is not allowed.'); end if length(L)>1 B = subsref(B,L(2:end));

@ReferAssignGetSet/subsasgn function B = subsasgn(A,L,RHS) ReAsGeSe = A(1); switch L(1).type case '.' if isequal(ReAsGeSe.PropertyAssign,'followSet') if length(L)==1 B = set(A,L(1).subs,RHS); else tmp = get(A,L(1)); tmp = subsasgn(tmp,L(2:end),RHS); B = set(A,L(1).subs,tmp); end error('dot(.) assignment not allowed. Use SET.'); othercases

@ReferAssignGetSet/subsasgn case '()' if isequal(ReAsGeSe.ParenAssign,'builtin') if length(L)==1 B = builtin('subsasgn',A,L(1),RHS); else tmp = builtin('subsref',A,L(1)); tmp = subsasgn(tmp,L(2:end),RHS); B = builtin('subsasgn',A,L(1),tmp); end elseif isequal(ReAsGeSe.ParenAssign,'ProgSupplied') B = ParenthesisAssign(A,L(1).subs,RHS); tmp = subsref(A,L(1)); B = ParenthesisAssign(A,L(1).subs,tmp); error('() assignment is not allowed.');

Examples using @point (try these) >> p = point(2,4); >> get(p) >> get(p,’Radius’) >> p.Angle >> p.An >> get(p,’Angle’) >> p.XCoord = 4; >> p.Angle*180/pi % convert to degrees >> set(p) >> p2 = point(-5,10); >> M = [p p2;point(1,1) point(3,-3)]; >> M(2,1).XCoord >> M(2,1).XCo = 3; >> M(2,1).An Why do these work? New Challenge: After you check these commands out, make Radius and Angle settable. Rule would be that when you change Radius, the Angle is unchanged, and similar for changing Angle. In each case, the underlying “real” data (X and Y) have to get correctly modified.