MATLAB Object-Oriented Programming

(Joyce) #1

If a class definition changes the property validation such that the loaded property value is
no longer valid, MATLAB substitutes the currently defined default value for that property.
However, the load function suppresses the validation errors that occur before assigning
the default value from the current class definition. Therefore, validation errors are
silently ignored during load operations.


To illustrate this behavior, this example creates, saves, and loads an object of the
MonthTemp class. This class restricts the AveTemp property to a cell array.


classdef MonthTemp
properties
AveTemp cell
end
end


Create a MonthTemp object and assign a value to the AveTemp property.


a = MonthTemp;
a.AveTemp = {'May',70};


Save the object using save.


save TemperatureFile a


Edit the property definition to change the validation class for the AveTemp property from
cell array to containers.Map.


classdef MonthTemp
properties
AveTemp containers.Map
end
end


Load the saved object with the new class definition on the MATLAB path. MATLAB cannot
assign the saved value to the AveTemp property because the cell array, {'May',70}, is
not compatible with the current requirement that the property value be a
containers.Map object. MATLAB cannot convert a cell array to a containers.Map.


To address the incompatibility, MATLAB sets the AveTemp property of the loaded object to
the current default value, which is an empty containers.Map object.


load TemperatureFile a
a.AveTemp


Validate Property Values
Free download pdf