Save and Load System Object
This example shows how to load and save a System object™.
Save System Object and Child Object
Define a saveObjectImpl
method to specify that more than just public
properties should be saved when the user saves a System object. Within this method, use the default
saveObjectImpl@matlab.System
to save public properties to the struct,
s
. Use the saveObject
method to save child objects.
Save protected and dependent properties, and finally, if the object has been called and not
released, save the object state.
methods (Access = protected) function s = saveObjectImpl(obj) s = saveObjectImpl@matlab.System(obj); s.child = matlab.System.saveObject(obj.child); s.protectedprop = obj.protectedprop; s.pdependentprop = obj.pdependentprop; if isLocked(obj) s.state = obj.state; end end end
Load System Object and Child Object
Define a loadObjectImpl
method to load a previously saved System object. Within this method, use the loadObject
to load the child
System object, load protected and private properties, load the state if the object was
called and not released, and use loadObjectImpl
from the base class to load
public properties.
methods (Access = protected) function loadObjectImpl(obj,s,isInUse) obj.child = matlab.System.loadObject(s.child); obj.protectedprop = s.protectedprop; obj.pdependentprop = s.pdependentprop; if isInUse obj.state = s.state; end loadObjectImpl@matlab.System(obj,s,isInUse); end end
Complete Class Definition Files with Save and Load
The Counter
class definition file sets up an object with a count
property. This counter is used in the MySaveLoader
class definition file
to count the number of child objects.
classdef Counter < matlab.System properties(DiscreteState) Count end methods (Access=protected) function setupImpl(obj, ~) obj.Count = 0; end function y = stepImpl(obj, u) if u > 0 obj.Count = obj.Count + 1; end y = obj.Count; end end end
classdef MySaveLoader < matlab.System properties (Access = private) child pdependentprop = 1 end properties (Access = protected) protectedprop = rand; end properties (DiscreteState = true) state end properties (Dependent) dependentprop end methods function obj = MySaveLoader(varargin) obj@matlab.System(); setProperties(obj,nargin,varargin{:}); end function set.dependentprop(obj, value) obj.pdependentprop = min(value, 5); end function value = get.dependentprop(obj) value = obj.pdependentprop; end end methods (Access = protected) function setupImpl(obj) obj.state = 42; obj.child = Counter; end function out = stepImpl(obj,in) obj.state = in + obj.state + obj.protectedprop + ... obj.pdependentprop; out = obj.child(obj.state); end end % Serialization methods (Access = protected) function s = saveObjectImpl(obj) % Call the base class method s = saveObjectImpl@matlab.System(obj); % Save the child System objects s.child = matlab.System.saveObject(obj.child); % Save the protected & private properties s.protectedprop = obj.protectedprop; s.pdependentprop = obj.pdependentprop; % Save the state only if object called and not released if isLocked(obj) s.state = obj.state; end end function loadObjectImpl(obj,s,isInUse) % Load child System objects obj.child = matlab.System.loadObject(s.child); % Load protected and private properties obj.protectedprop = s.protectedprop; obj.pdependentprop = s.pdependentprop; % Load the state only if object is in use if isInUse obj.state = s.state; end % Call base class method to load public properties loadObjectImpl@matlab.System(obj,s,isInUse); end end end