MATLAB Object-Oriented Programming

(Joyce) #1

end
properties (Dependent)
Location
end
properties (Hidden)
OfficeNumber
end
methods
function obj = set.Location(obj,val)
obj.OfficeNumber = val;
end
function val = get.Location(obj)
val = obj.OfficeNumber;
end
end
end


Update Property When Loading


Suppose that you modify a class so that a property value changes in its form or type.
Previously saved objects of the class must be updated when loaded to have a conforming
property value.


Consider a class that has an AccountID property. Suppose that all account numbers must
migrate from eight-digit numeric values to 12-element character arrays.


You can accommodate this change by implementing a loadobj method.


The loadobj method:



  • Tests to determine if the load function passed a struct or object. All loadobj
    methods must handle both struct and object when there is an error in load.

  • Tests to determine if the AccountID number contains eight digits. If so, change it to a
    12-element character array by calling the paddAccID method.


After updating the AccountID property, loadobj returns a MyAccount object that
MATLAB loads into the workspace.


classdef MyAccount
properties
AccountID
end
methods


Maintain Class Compatibility
Free download pdf