MATLAB Object-Oriented Programming

(Joyce) #1

  • When the loadobj method sets the Address property, it invokes the property set
    method (set.Address), which extracts the substrings required by the
    StreetAddress, City, State, and ZipCode properties.

  • The Transient (not saved) property SaveInOldFormat enables you to specify
    whether to save the version 2 object as a struct or an object.


classdef PhoneBookEntry
properties
Name
StreetAddress
City
State
ZipCode
PhoneNumber
end
properties (Constant)
Sep = ', '
end
properties (Dependent, SetAccess=private)
Address
end
properties (Transient)
SaveInOldFormat = false;
end
methods (Static)
function obj = loadobj(s)
if isstruct(s)
obj = PhoneBookEntry;
obj.Name = s.Name;
obj.Address = s.Address;
obj.PhoneNumber = s.PhoneNumber;
else
obj = s;
end
end
end
methods
function address = get.Address(obj)
address = [obj.StreetAddress,obj.Sep,obj.City,obj.Sep,...
obj.State,obj.Sep,obj.ZipCode];
end
function obj = set.Address(obj,address)
addressItems = regexp(address,obj.Sep,'split');
if length(addressItems) == 4

13 Saving and Loading Objects

Free download pdf