Implement a Simple Algorithm
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 model using a MATLAB System block.
Open and Simulate the Model
This model has a MATLAB System block that uses 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 Scope block displays the output along with the input. When you run the model, you can see in the Scope block that the input to MATLAB System block is multiplied by two.
System Object Class Definition
You can access MATLAB source code used by the MATLAB System block by clicking the Source Code hyperlink in the Block Parameters dialog box. 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 Box
The MATLAB System block displays the name of the System object TimesTwo
on the block and uses the input and output variable names from the stepImpl
method of the TimesTwo
class as port labels. If you open the MATLAB System Block Parameters dialog box by double clicking the block, the dialog box shows title as TimesTwo
and the description Multiply input by 2
. The title comes from the name of the System object, and the description is created from the class help summary in the System object.