Quantcast

Documentation Center

  • Trial Software
  • Product Updates

Clone System Object

This example shows how to clone a System object™.

Clone System Object

You can define your own clone method, which is useful for copying objects without saving their state. The default cloneImpl method copies both a System object™ and its current state. If an object is locked, the default cloneImpl creates a cloned object that is also locked. An example of when you may want to write your own clone method is for cloning objects that handle resources. These objects cannot allocate resources twice and you would not want to save their states. To write your clone method, use the saveObject and loadObject methods to perform the clone within the cloneImpl method.

methods(Access=protected)
   function obj2 = cloneImpl(obj1)
      s = saveObject (obj1);
      obj2 = loadObject(s);
   end
end

Complete Class Definition File with Clone

classdef PassThrough < matlab.System
  methods (Access=protected)
    function y = stepImpl(~,u)
        y = u;
    end
    function obj2 = cloneImpl(obj1)
        s = matlab.System.saveObject(obj1);
        obj2 = matlab.System.loadObject(s);
    end
  end
end

See Also

| |

Was this topic helpful?