Main Content

System Design in Simulink Using System Objects

System Design and Simulation in Simulink

You can use System objects in your model to simulate in Simulink®.

  1. Create a System object™ to be used in your model. See Define New System Objects for Use in Simulink for information.

  2. Test your new System object in MATLAB®. See Test New System Objects in MATLAB.

  3. Add the System object to your model by using the MATLAB System block. See Add System Objects to Your Simulink Model for information.

  4. Add other Simulink blocks as needed and connect the blocks to construct your system.

  5. Run the system.

Define New 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 Author Blocks Using MATLAB System Objects.

Define System Object with Block Customizations

Create a System object for use in Simulink. The example performs system identification using a least mean squares (LMS) adaptive filter.

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 box 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 matlab.System. 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.

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 method 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 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 box.

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

  • 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
   % 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

Create a System object for use in Simulink. The example performs system identification using a least mean squares (LMS) adaptive filter and uses feedback loops.

Create a class definition text file to define your System object. The code in this example creates an integer delay and includes feedback loops, and customizations to the block icon. For information on feedback loops, see Use System Objects in Feedback Loops. This example implements a System object that you can use for nondirect feedthrough.

On the first line of the class definition file, subclass from matlab.System. 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.

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.

  • Use the setupImpl method to initialize some of the object’s properties.

  • Use the resetImpl method to reset the property states.

  • Use the validatePropertiesImpl method to check that the property values are valid.

Add the following 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 method 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
   % 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 object multiple times with different inputs. Doing this step tests for syntax errors and other possible issues before you add it to Simulink. For example,

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

Add System Objects to Your Simulink Model

System Objects in the MATLAB Function Block

You can include System object code in Simulink models with the MATLAB Function block. Your function can include one or more System objects. Portions of your system may be easier to implement in the MATLAB environment than directly in Simulink. Many System objects have Simulink block counterparts with equivalent functionality. Before writing MATLAB code to include in a Simulink model, check for existing blocks that perform the desired operation.

System Objects in the MATLAB System Block

You can include individual System objects that you create with a class definition file into Simulink with the MATLAB System block. This option is one way to add your own algorithm blocks into your Simulink models.

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

For information, see Author Blocks Using MATLAB System Objects.