Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Reset Algorithm and Release Resources

Reset Algorithm State

This example shows how to reset an object state.

Reset Counter to Zero

pCount is an internal counter property of the System object™ obj. The user calls the reset method on the locked object, which calls the resetImpl method. In this example, pCount resets to 0.

Note

When resetting an object’s state, make sure you reset the size, complexity, and data type correctly.

methods (Access = protected)
   function resetImpl(obj)
      obj.pCount = 0; 
   end
end 

Complete Class Definition File with State Reset

classdef Counter < matlab.System
% Counter System
        object that increments a counter

   properties (Access = private)
      pCount
   end
   
   methods (Access = protected)
      % Increment the counter and return 
      % its value as an output
      function c = stepImpl(obj)
          obj.pCount = obj.pCount + 1;
          c = obj.pCount;
      end

      % Reset the counter to zero.
      function resetImpl(obj)
         obj.pCount = 0; 
      end
   end 
end

Release System Object Resources

This example shows how to release resources allocated and used by the System object. These resources include allocated memory, files used for reading or writing, etc.

Release Memory by Clearing the Object

This method allows you to clear the axes on the Whiteboard figure window while keeping the figure open.

methods
   function releaseImpl(obj)
      cla(Whiteboard.getWhiteboard());
      hold on
   end
end

Complete Class Definition File with Released Resources

classdef Whiteboard < matlab.System
% Whiteboard Draw lines on a figure window
%
% This System
        object shows the use of StringSets
%
   properties
     Color = 'blue'
   end

   properties (Hidden)
     % Let user choose a color
     ColorSet = matlab.system.StringSet({'red','blue','green'});
   end

   methods (Access = protected)
     function stepImpl(obj)
         h = Whiteboard.getWhiteboard();
         plot(h, ...
           randn([2,1]), randn([2,1]), ...
           'Color',obj.Color(1));
     end

     function releaseImpl(obj)
         cla(Whiteboard.getWhiteboard());
         hold on
     end
   end

   methods (Static)
     function a = getWhiteboard()
         h = findobj('tag','whiteboard');
         if isempty(h)
           h = figure('tag','whiteboard');
           hold on
         end
         a = gca;
     end
   end
end

See Also

|

Related Topics

Was this topic helpful?