MATLAB Object-Oriented Programming

(Joyce) #1

Improve Version Compatibility with Default Values


In this section...
“Version Compatibility” on page 13-10
“Using a Default Property Value” on page 13-10

Version Compatibility


Default property values can help you implement version compatibility for saved objects.
For example, suppose that you add a property to version 2 of your class. Having a default
value enables MATLAB to assign a value to the new property when loading a version 1
object.

Similarly, suppose version 2 of your class removes a property. If a version 2 object is
saved and loaded into version 1, your loadobj method can use the default value from
version 1.

Using a Default Property Value


The EmployeeInfo class shows how to use property default values as a way to enhance
compatibility among versions. Version 1 of the EmployeeInfo class defines three
properties — Name, JobTitle, and Department.

classdef EmployeeInfo
properties
Name
JobTitle
Department
end
end

Version 2 of the EmployeeInfo class adds a property, Country, for the country name of
the employee location. The Country property has a default value of 'USA'.

classdef EmployeeInfo
properties
Name
JobTitle
Department
Country = 'USA'

13 Saving and Loading Objects

Free download pdf