System Design in Simulink Using System Objects

Define New Kinds of System Objects for Use in Simulink

A System object™ is a component you can use to create your system in MATLAB. You can write the code in MATLAB® and use that code to create a block in Simulink®. To define your own System object, you write a class definition file, which is a text-based MATLAB file that contains the code defining your object. See System Object Integration in the Simulink documentation.

Define System Object with Block Customizations

This example shows how to create a System object for use in Simulink. The example performs system identification using a least mean squares (LMS) adaptive filter and is similar to the System Identification Using MATLAB System BlocksSystem Identification Using MATLAB System Blocks Simulink example.

This example shows how to create a class definition text file to define your System object. The code in this example creates a least mean squares (LMS) filter and includes customizations to the block icon and dialog appearance.

    Note:   Instead of manually creating your class definition file, you can use the New > System Object > Simulink Extension menu option to open a template. This template includes customizations of the System object for use in the Simulink MATLAB System block. You edit the template file, using it as guideline, to create your own System object.

On the first line of the class definition file, specify the name of your System object and subclass from both matlab.System and matlab.system.mixin.CustomIcon. The matlab.System base class enables you to use all the basic System object methods and specify the block input and output names, title, and property groups. The CustomIcon mixin class enables the method that lets you specify the block icon.

Add the appropriate basic System object methods to set up, reset, set the number of inputs and outputs, and run your algorithm. See the reference pages for each method and the full class definition file below for the implementation of each of these methods.

  • Use the setupImpl method to perform one-time calculations and initialize variables.

  • Use the stepImpl method to implement the block's algorithm.

  • Use the resetImpl to reset the state properties or DiscreteState properties.

  • Use the getNumInputsImpl and getNumOutputsImpl methods to specify the number of inputs and outputs, respectively.

Add the appropriate CustomIcon methods to define the appearance of the MATLAB System block in Simulink. See the reference pages for each method and the full class definition file below for the implementation of each of these methods.

  • Use the getHeaderImpl method to specify the title and description to display on the block dialog.

  • Use the getPropertyGroupsImpl method to specify groups of properties to display on the block dialog.

  • Use the getIconImpl method to specify the text to display on the block icon.

  • Use the getInputNamesImpl and getOutputNamesImpl methods to specify the labels to display for the block input and output ports.

The full class definition file for the least mean squares filter is:

classdef lmsSysObj < matlab.System &...
      matlab.system.mixin.CustomIcon
   % lmsSysObj Least mean squares (LMS) adaptive filtering. 
   % #codegen

   properties
      % Mu Step size
      Mu = 0.005;
   end

   properties (Nontunable)
      % Weights  Filter weights
      Weights = 0;
      % N  Number of filter weights
      N = 32;
   end
  
   properties (DiscreteState) 
      X;
      H;
   end
  
   methods (Access = protected)
      function setupImpl(obj)
         obj.X = zeros(obj.N,1);
         obj.H = zeros(obj.N,1);
      end
      
      function [y, e_norm] = stepImpl(obj,d,u)
         tmp = obj.X(1:obj.N-1);
         obj.X(2:obj.N,1) = tmp;
         obj.X(1,1) = u;
         y = obj.X'*obj.H;
         e = d-y;
         obj.H = obj.H + obj.Mu*e*obj.X;
         e_norm = norm(obj.Weights'-obj.H);
      end
    
      function resetImpl(obj)
         obj.X = zeros(obj.N,1);
         obj.H = zeros(obj.N,1);
      end
      
   end   

   % Block icon and dialog customizations
   methods (Static, Access = protected)
      function header = getHeaderImpl
         header = matlab.system.display.Header(...
              'lmsSysObj', ...
              'Title', 'LMS Adaptive Filter');
      end
      
      function groups = getPropertyGroupsImpl
         upperGroup = matlab.system.display.SectionGroup(...
              'Title','General',...
              'PropertyList',{'Mu'});
            
         lowerGroup = matlab.system.display.SectionGroup(...
              'Title','Coefficients', ...
              'PropertyList',{'Weights','N'});
            
         groups = [upperGroup,lowerGroup];
      end
   end
   
   methods (Access = protected)
      function icon = getIconImpl(~)
         icon = sprintf('LMS Adaptive\nFilter');
      end
      function [in1name, in2name] = getInputNamesImpl(~)
         in1name = 'Desired';
         in2name = 'Actual';
      end
      function [out1name, out2name] = getOutputNamesImpl(~)
         out1name = 'Output';
         out2name = 'EstError';
      end
   end
end

Define System Object with Nondirect Feedthrough

This example shows how to create a System object for use in Simulink. The example performs system identification using a least mean squares (LMS) adaptive filter and is similar to the System Identification Using MATLAB System BlocksSystem Identification Using MATLAB System Blocks Simulink example.

This example shows how to create a class definition text file to define your System object. The code in this example creates an integer delay and includes customizations to the block icon. It implements a System object that you can use for nondirect feedthrough. SeeUse System Objects in Feedback Loops for more information.

On the first line of the class definition file, subclass from matlab.System, matlab.system.mixin.CustomIcon, and matlab.system.mixin.Nondirect. The matlab.System base class enables you to use all the basic System object methods and specify the block input and output names, title, and property groups. The CustomIcon mixin class enables the method that lets you specify the block icon. The Nondirect mixin enables the methods that let you specify how the block is updated and what it outputs.

Add the appropriate basic System object methods to set up and reset the object and set and validate the properties. Since this object supports nondirect feedthrough, you do not implement the stepImpl method. You implement the updateImpl and outputImpl methods instead. See the reference pages for each method and the full class definition file below for the implementation of each of these methods.

Add the following Nondirect mixin class methods instead of the stepImpl method to specify how the block updates its state and its output. See the reference pages and the full class definition file below for the implementation of each of these methods.

  • Use the outputImpl method to implement code to calculate the block output.

  • Use the updateImpl method to implement code to update the block's internal states.

  • Use the isInputDirectFeedthroughImpl to specify that the block is not direct feedthrough. Its inputs do not directly affect its outputs.

Add the getIconImpl method to define the block icon when it is used in Simulink via the MATLAB System block. See the reference page and the full class definition file below for the implementation of this method.

The full class definition file for the delay is:

classdef intDelaySysObj < matlab.System &...
     matlab.system.mixin.Nondirect &...
     matlab.system.mixin.CustomIcon
   % intDelaySysObj Delay input by specified number of samples.
   % #codegen

   properties
      % InitialOutput Initial output
      InitialOutput = 0;
   end

   properties (Nontunable)
      % NumDelays Number of delays
      NumDelays = 1;
   end

   properties (DiscreteState)
      PreviousInput;
   end

   methods (Access = protected)
      function setupImpl(obj, ~)
         obj.PreviousInput = ones(1,obj.NumDelays)*obj.InitialOutput;
      end
      
      function [y] = outputImpl(obj, ~)
         % Output does not directly depend on input
         y = obj.PreviousInput(end);
      end

      function updateImpl(obj, u)
         obj.PreviousInput = [u obj.PreviousInput(1:end-1)]; 
      end

      function flag = isInputDirectFeedthroughImpl(~,~)
         flag = false;
      end

      function validatePropertiesImpl(obj)
         if ((numel(obj.NumDelays)>1) || (obj.NumDelays <= 0))
            error('Number of delays must be positive non-zero scalar value.');
         end
         if (numel(obj.InitialOutput)>1)
            error('Initial output must be scalar value.');
         end
      end

      function resetImpl(obj)
         obj.PreviousInput = ones(1,obj.NumDelays)*obj.InitialOutput;
      end
      
      function icon = getIconImpl(~)
         icon = sprintf('Integer\nDelay');
      end
   end
end

Test New System Objects in MATLAB

  1. Create an instance of your new System object. For example, create an instance of the lmsSysObj.

    s = lmsSysObj;
  2. Run the step method on the object multiple times with different inputs. This tests for syntax errors and other possible issues before you add it to Simulink. For example,

    desired = 0;
    actual = 0.2;
    step(s,desired,actual);

Add System Objects to Your Simulink Model

  1. Add your System objects to your Simulink model by using the MATLAB System block as described in Mapping System Objects to Block Dialog Box.

  2. Add other Simulink blocks, connect them, and configure any needed parameters to complete your model as described in the Simulink documentation. See the System Identification for an FIR System Using MATLAB System BlocksSystem Identification for an FIR System Using MATLAB System Blocks Simulink example.

  3. Run your model in the same way you run any Simulink model.

Was this topic helpful?