Main Content

Implement a Simple Algorithm

Introduction

This example shows how to use a simple System object in Simulink with the MATLAB System block.

System objects

System objects allow you to implement algorithms using MATLAB. System objects are a specialized kind of MATLAB object, designed specifically for implementing and simulating dynamic systems with inputs that change over time.

After you define a System object, you can include it in a Simulink model using a MATLAB System block.

Model Description

This model has a MATLAB System block using the System object TimesTwo that multiples the input by two. The input to the MATLAB System block is provided by the Sine Wave block. The output along with the input is displayed in the Scope block. When you run the model, you can see that the input to MATLAB System block is multiplied by two in the Scope block.

System Object Class Definition

You can access MATLAB source code used by the MATLAB System block by clicking the "Source Code" hyperlink from the block dialog. The System object implements only the stepImpl method. The algorithm does not need any properties or additional methods.

classdef TimesTwo < matlab.System
%TimesTwo Multiply input by 2
%   obj = TimesTwo returns a System object, obj, that 
%   multiples its input by two.

    methods(Access = protected)
        function y = stepImpl(~, u)
            y = 2 * u;
        end
    end
end

MATLAB System Block Icon and Dialog

The MATLAB System block displays the name of the System object TimesTwo on the block and uses the input and output variable names from stepImpl method of the TimesTwo class as port labels. If you open the MATLAB System block dialog by double clicking on the block, the dialog shows title as TimesTwo and a description as "Multiply input by 2" as shown below. The title comes from the name of the System object used and the description is created from the class help summary in the System object.

Related Topics